我正在嘗試撰寫一個腳本,以便在我的域中獲取所有機器。這是我到目前為止發現的內容,但是我需要添加其他資訊,但仍然無法獲得正確的資訊以退出。如果有人可以幫助我,那就太好了。
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。
uj5u.com熱心網友回復:
Active Directory 計算機物件不包含 MAC 地址屬性,因此您將無法僅使用 Active Directory 物件獲取所需的資訊;但是,您可以使用 AD 計算機物件的“IPv4Address”屬性并查詢 DHCP 服務器以查找機器 MAC 地址并將輸出資料放置為“custompsobject”,然后將結果匯出為 CV 表。此外,如果您有系統中心配置管理器“SCCM”,您可以查詢其資料庫以生成包含所有所需資料(名稱、作業系統、作業系統版本、作業系統服務包、IPv4 地址和 MAC 地址)的報告
uj5u.com熱心網友回復:
您需要在計算機上回圈并在回圈內單獨獲取 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393822.html
