如何检索LAN适配器MAC地址?

时间:2015-06-11 10:19:46

标签: windows vbscript

我想从Windows系统检索MAC地址,仅用于LAN适配器。你能告诉我如何在VBScript中处理这个问题吗?

我目前正在使用这个VBScript来获取MAC地址,但这给了我所有适配器的结果,而我在连接LAN适配器时只需要MAC地址。

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

3 个答案:

答案 0 :(得分:1)

这个怎么样

方式1:

如果可能,尝试排除所有不需要的适配器(排除VmWare和VirtualBox)。在一些计算机上,可能是更具体的适配器,您需要找出并排除

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

方式2:

找到具有特定网关的所有适配器

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

答案 1 :(得分:1)

使用Win32_NetworkAdapter类而不是Win32_NetworkAdapterConfiguration类。后者没有提供适配器名称的属性。

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

答案 2 :(得分:0)

试试这个,你将获得LAN Adapter的MAC地址,

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

我只使用此代码且工作正常。