我試圖獲取計算機串列并從中提取資訊并將其匯出到 Excel 表中,其中我擁有每行系統的所有資訊。我試圖獲取如下資訊:
- 計算機名
- 作業系統
- BIOS
- 上次啟動
- 等等
我寫的代碼:
$computers = Get-Content "C:\example\Computers.txt)"
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object PSComputerName, Manufacturer, SerialNumber, SMBIOSBIOSVersion
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | select csname, lastbootuptime
$objects =$OSinfo =$BIOSinfo =$lastboot
$objects | Export-csv -Path "C:\example\output.csv"
但是,我不知道如何將所有這些資訊放入一個電子表格選項卡中。我也不知道如何告訴腳本是否無法 ping 或找到它,只說“離線”
uj5u.com熱心網友回復:
這應該適合你:
$computers = Get-Content "C:\example\Computers.txt"
# Define the properties we want to select from each query
$osColumns = 'PSComputerName', 'Caption', 'OSArchitecture', 'Version', 'BuildNumber'
$biosColumns = 'PSComputerName', 'Manufacturer', 'SerialNumber', 'SMBIOSBIOSVersion'
$lastbootColumns = 'csname', 'lastbootuptime'
# Obtain the desired information from Wmi/Cim
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select-Object $osColumns
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object $biosColumns
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | Select-Object $lastbootColumns
# Iterate over the computers and collect the computer-specific information
$computerInfo = foreach( $computer in $computers ) {
$thisOSInfo = $OSInfo | Where-Object { $_.PSComputerName -eq $computer }
$thisBIOSInfo = $BIOSinfo | Where-Object { $_.PSComputerName -eq $computer }
$thisLastboot = $lastboot | Where-Object { $_.csname -eq $computer }
# This row object will be returned as a PSCustomObject for export
$row = @{}
foreach( $prop in @( $thisOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $osColumns ) {
$row[$prop] = $thisOSInfo.$prop
}
}
foreach( $prop in @( $thisBIOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $biosColumns ) {
$row[$prop] = $thisBIOSInfo.$prop
}
}
foreach( $prop in @( $thisLastboot.psobject.Properties.Name ) ) {
if( $prop -in $lastbootColumns ) {
$row[$prop] = $thisLastboot.$prop
}
}
[PSCustomObject]$row
}
$computerInfo | Export-Csv -NoTypeInformation \path\to\report.csv
它的作業方式:
- 從每個查詢中明確定義我們想要的列。我們稍后將使用它們來確保我們只從結果查詢回應中獲取我們想要的屬性。請注意,
Get-CimInstance將具有csname屬性而不是PSComputerName. - 查詢 WMI 類以獲取有關已知計算機的資訊,將每個回應存盤在其自己的變數中。
- 遍歷每臺查詢的計算機,從每個查詢回應中構建該行資訊。
- 僅當相應
*Columns變數中存在查詢屬性時才添加它。這對您的代碼來說不是必需的,但在匯出其他一些非 WMI/Cim 物件時,或者如果您不Select-Object早先使用,以減少噪音時變得有必要。
- 僅當相應
- Each row is defined as a hastable for ease of modification, then converted to a
PSCustomObjectto it behaves more like an immutable object upon return. This is necessary for the output ofExport-Csvto output the way you want. - Export the computer information with
Export-Csvto a CSV file, which is now an array ofPSCustomObjectswhere each row should be a single computer you've queried.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456773.html
下一篇:將命令列的輸出重定向到C中的變數
