Reputation: 49
I want to retrieve the MAC address from a Windows system, only for LAN Adapter. Can you suggest to me how I'd handle this in VBScript?
I'm currently using this VBScript for getting the MAC address, but this is giving me results for all adapters, while I only want the MAC address when I am connected with LAN Adapter.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
If objItem.ServiceName <> "VMnetAdapter" AND isNull(objItem.MACAddress)=0 Then
Wscript.Echo objItem.MACAddress
Wscript.Echo objItem.ServiceName
End if
Next
Upvotes: 2
Views: 2391
Reputation: 83
Try this,you will get MAC Address of LAN Adapter Only,
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_NetworkAdapter WHERE (NetConnectionID like '%Local Area Connection%')"
For Each adapter In wmi.ExecQuery(qry)
If Not IsNull(adapter.MACAddress) Then Wscript.Echo adapter.MACAddress
Next
I am using this code only and its working fine.
Upvotes: 0
Reputation: 200323
Use the Win32_NetworkAdapter
class instead of the Win32_NetworkAdapterConfiguration
class. The latter doesn't have a property providing the adapter name.
adaptername = "LAN Adapter"
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & adaptername & "'"
For Each adapter In wmi.ExecQuery(qry)
If Not IsNull(adapter.MACAddress) Then Wscript.Echo adapter.MACAddress
Next
Upvotes: 1
Reputation: 2940
how about this
way 1:
if possible try to exclude all not required adapters (excluded VmWare and VirtualBox). of couse, on some computers could be more specific adapters which you need to find out and exclude
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
if objItem.ServiceName <> "VMnetAdapter" and objItem.ServiceName <> "VBoxNetAdp" and objItem.ServiceName <> "" and isNull(objItem.MACAddress) = 0 Then
Wscript.Echo objItem.ServiceName & vbCrLf & objItem.MACAddress
End if
Next
way 2:
find all adapters which has specific gateway
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
if objItem.ServiceName <> "VMnetAdapter" and objItem.ServiceName <> "VBoxNetAdp" and objItem.ServiceName <> "" and isNull(objItem.MACAddress) = 0 Then
For Each strIP in objItem.DefaultIPGateway
If strIP = "192.168.1.1" Then
Wscript.Echo objItem.ServiceName & vbCrLf & objItem.MACAddress
End If
Next
End if
Next
https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx
Upvotes: 1