【问题标题】:How to find mac address in active directory如何在活动目录中找到mac地址
【发布时间】:2014-06-17 22:50:08
【问题描述】:

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

【问题讨论】:

  • 我不认为computer object 包含MAC 地址,这是计算机信息存储在活动目录中的位置。
  • 虽然 AD does contain an attribute 用于 MAC 地址,但它似乎没有填充到我当前的域中。
  • 我相信 SCCM 客户端有这个选项,但它似乎不在范围内。这是一些“伪算法”,可以帮助您编写脚本。 1. 列出您要测试的所有计算机对象 2. 在 Powershell 中对所有计算机对象运行查询(通过获取 ipconfig.exe 或 Win32_NetworkAdapter) 3. 使用结果相应地更新 OU 对象。这只是脚本的“存根”,我建议您尝试编写它并在遇到问题时返回这里:-)
  • 冗长的方法:1) 从 AD 获取计算机名称,2) nslookup 或 [System.Net.Dns]::GetHostEntry 他们的 IP 并存储在 var 或哈希表中 3) 将上一步的输出与 @987654324 的输出进行比较@
  • 您可能会更幸运地从 AD 中获取 OU 中的计算机名称列表,然后将其带回您的 DHCP 服务器以从租约信息中获取 MAC 地址。

标签: powershell active-directory ldap mac-address


【解决方案1】:

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

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

还要考虑很多设备可以有多个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

【讨论】:

  • 感谢您的所有意见。我认为基于每个人的建议的首选选项将尝试将 AD 中的 OU 中的对象与 DHCP 匹配。对此的任何提示将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 2012-03-16
  • 2012-07-24
  • 1970-01-01
  • 2018-05-03
相关资源
最近更新 更多