如何找到 Active Directory 计算机对象的 MAC 地址?

How to find the MAC Address of Active Directory Computer Objects?

我正在尝试编写一个脚本,让我可以在我的域中获取所有机器。这是我到目前为止发现的,但是我需要添加额外的信息,但仍然无法获得正确的信息来提取。如果有人能帮助我,那就太好了。

Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -Properties  Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address| ft -Wrap –Auto

我仍然需要能够从所有机器以及机器所属的域中获取 MAC 地址。更糟糕的是,我需要弄清楚如何将所有数据导出到 CSV。

Active directory 计算机对象不包含 MAC 地址属性,因此仅使用 active directory 对象将无法获得所需的信息;但您可以使用 AD 计算机对象的“IPv4Address”属性并查询 DHCP 服务器以查找计算机 MAC 地址并将输出数据放置为“custompsobject”,然后将结果导出为 C.Vsheet。 此外,如果您有系统中心配置管理器“SCCM”,您可以查询其数据库以生成包含所有需要数据(名称、操作系统、操作系统版本、操作系统服务包、IPv4 地址和 MAC 地址)的报告

您将需要遍历计算机并在循环内单独获取 MAC 地址:

# Get-ADComputer returns these properties by default: 
# DistinguishedName, GroupCategory, GroupScope, Name, ObjectClass, ObjectGUID, SamAccountName, SID

$props  = 'OperatingSystem', 'OperatingSystemVersion', 'OperatingSystemServicePack', 'IPv4Address'
$result = Get-ADComputer -Filter "operatingsystem -like '*Windows server*' -and enabled -eq 'true'" -Properties  $props | 
    Sort-Object OperatingSystem | ForEach-Object {
        $mac = if ((Test-Connection -ComputerName $_.Name -Count 1 -Quiet)) {
            # these alternative methods could return an array of MAC addresses

            # get the MAC address using the IPv4Address of the computer
            (Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $_.IPv4Address).MACAddress
            # or use
            # Invoke-Command -ComputerName $_.IPv4Address -ScriptBlock { (Get-NetAdapter | Where-Object {$_.Status -eq 'Up'}).MacAddress }
            # or 
            # (& getmac.exe /s $_.IPv4Address /FO csv | ConvertFrom-Csv | Where-Object { $_.'Transport Name' -notmatch 'disconnected' }).'Physical Address'
        }
        else { $mac = 'Off-Line' }

        # return the properties you want as object
        $_ | Select-Object Name, OperatingSystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address,
                           @{Name = 'MacAddress'; Expression = {@($mac)[0]}}
    }

# output on screen
$result | Format-Table -AutoSize -Wrap

# or output to CSV file
$result | Export-Csv -Path 'X:\Wherever\ADComputers.csv' -NoTypeInformation -UseCulture