如何在活动目录中查找mac地址

时间:2014-06-17 14:20:46

标签: powershell active-directory ldap mac-address

您可以使用Powershell从Active Directory获取mac地址吗?我正在寻找一种方法来搜索特定OU中的MAC地址,如果可能的话。总的来说,我想要一种动态的方法来为连接到域的计算机找到mac地址,即使它们被关闭了,我认为AD可能是一个很好的方法,如果可能的话。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

正如评论所说,该信息不在Active Directory中保存。

考虑使用计算机启动脚本使用mac地址填充AD中的字段。

还要考虑许多设备可以有多个mac地址,有些笔记本电脑可能有3个甚至。

这是基于我使用的脚本(在VBScript中)的示例:

Option Explicit

Dim objRootDSE, objNetwork, objWMIService, objComputer
Dim strComputer, strMacAddresses
Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
Dim adoConnection, adoRecordset

strComputer = "."
strMacAddresses = ""

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where AdapterType = 'Ethernet 802.3' OR AdapterType = 'Wireless'")

strMacAddresses = ""

If Not colNetworkAdapterConfiguration Is Nothing Then
    For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
        If strMacAddresses <> "" Then
            strMacAddresses = strMacAddresses & " "
        End If
        strMacAddresses = strMacAddresses & Trim(objNetworkAdapterConfiguration.MACAddress)
    Next
End If

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
    WScript.Quit
End If

Set adoRecordset = adoConnection.Execute("<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
    WScript.Quit
End If

If Not adoRecordset.EOF Then
    Set objComputer = GetObject(adoRecordset.Fields(0).Value)
    objComputer.Put "extensionAttribute1", strMacAddresses
    objComputer.SetInfo
End If

If Err.Number <> 0 Then
    WScript.Quit
End If