Wednesday, August 31, 2005

WMI Mac address

Q. How can I use Windows Management Instrumentation (WMI) to enumerate MAC
addresses on a machine?

A. You can enumerate a machine's list of network adapters and find the
associated MAC address. The following sample code, which you can
download at
http://list.windowsitpro.com/t?ctl=12191:24641 , prints out
the network adapter name and the MAC address. Because of space limitations,
some lines wrap to two lines.

Dim objNetworkAdapters, objAdapter, objWMI

Set objWMI = Nothing
Set objWMI = GetObject("winmgmts:")

' Get a list of IP-enabled adapters.
Set objNetworkAdapters = objWMI.ExecQuery("select * from " & _
"Win32_NetworkAdapterConfiguration where IPEnabled = 1")

For Each objAdapter In objNetworkAdapters
wscript.echo "Network adapter: " & objAdapter.Caption & " has MAC " & _
"address " & objAdapter.MacAddress
Next

Here's a sample execution output showing two adapters on the machine:

D:\temp>cscript getmac.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Network adapter: [00000009] Broadcom 570x Gigabit Integrated Controller has
MAC address 00:0F:1F:25:DC:76
Network adapter: [00000014] Intel(R) PRO/Wireless 2200BG Network Connection
has MAC address 00:0E:35:6E:2C:B4

No comments: