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



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

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。

您需要在计算机上循环,并在循环中单独获取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:WhereverADComputers.csv' -NoTypeInformation -UseCulture

Active directory计算机对象不包含MAC地址属性,因此您将无法仅使用Active directory对象获得所需的信息;但是相反,您可以使用";IPv4Address";属性,并查询DHCP服务器以找到机器MAC地址并将输出数据作为"地址";自定义对象";然后将结果导出为C.V表。此外,如果您有System center configuration manager(系统中心配置管理器(;SCCM";您可以查询其数据库以生成包含所有所需数据(名称、操作系统、操作系统版本、操作系统Service Pack、IPv4地址和MAC地址(的报告

相关内容

最新更新