新手問題在這里。我在下面有這個用于 powershell 的腳本,它為我提供了來自 AD 的 PC 資訊。但我一直無法將結果匯出到 csv。還如何獲取 ipv4 ip 地址而不包括 ipv6?并說如果 PC 處于離線狀態,是否可以再給我一列以顯示如果它處于離線狀態,它會回傳 RPC 服務器不可用或離線。在此處輸入影像描述 感謝幫助。
$ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system
Clear-Host
foreach ($Computer in $ArrComputers)
{
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer
write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"-------------------------------------------------------"
"Manufacturer: " $computerSystem.Manufacturer
"Model: " $computerSystem.SystemFamily
"Machine Name: " $computerSystem.DNSHostName
"IP Address: " $computerIPv4.Ipaddress
"Serial Number: " $computerBIOS.SerialNumber
"CPU: " $computerCPU.Name
"HDD Capacity: " "{0:N2}" -f ($computerHDD.Size/1GB) "GB"
"HDD Space: " "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) " Free (" "{0:N2}" -f ($computerHDD.FreeSpace/1GB) "GB)"
"RAM: " "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) "GB"
"Operating System: " $computerOS.caption " " $computerOS.BuildNumber
"User logged In: " $computerSystem.UserName
"Last Reboot: " $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"-------------------------------------------------------"
}
uj5u.com熱心網友回復:
將變數分配給 foreach 并將結果輸出到 CSV 檔案。以下代碼未經測驗,但應該可以作業。使用 PscustomObject 將使資料結構化,并以一種簡單的方式處理資料。要了解更多資訊,請參閱 https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.2
要在線或離線查找 PC,您可以使用 Test-Connection 并將結果保存在 pscustomobject 中。
$ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system
Clear-Host
$output = foreach ($Computer in $ArrComputers) {
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer
[PSCustomObject]@{
"Manufacturer" = $computerSystem.Manufacturer
"Model" = $computerSystem.SystemFamily
"Machine Name" = $computerSystem.DNSHostName
"IP Address" = $computerIPv4.Ipaddress
"Serial Number" = $computerBIOS.SerialNumber
"CPU" = $computerCPU.Name
"HDD Capacity" = "{0:N2}" -f ($computerHDD.Size / 1GB) "GB"
"HDD Space" = "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) "Free(" "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) "GB)"
"RAM" = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) "GB"
"Operating System" = $computerOS.caption " " $computerOS.BuildNumber
"User logged In" = $computerSystem.UserName
"Last Reboot" = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
}
}
$output | Export-csv C:\Temp\outputfilename.csv -NoTypeInformation
uj5u.com熱心網友回復:
很多問題:
首先對于回圈開始時的 RPC 錯誤嘗試建立一個cimsession:
$cimSession = New-CimSession -ComputerName $Computer
if ($cimSession -eq $null)
{
write-host "Pas Glop $Computer"
continue
}
至少
對于 IPV4:
$IpAddressV4 = (Get-NetIPConfiguration -CimSession $cimSession | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" }).IPv4Address.IPAddress
將所有內容放入 CSV 檔案中:
$lines = @()
foreach ($Computer in $ArrComputers)
{
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$line = New-Object -TypeName PSCustomObject -Property @{"Computer" = $Computer;"Manufacturer" = $($computerSystem.Manufacturer)}
$lines = $line
}
$lines | Export-Csv "PathToYourCSVFile" -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/407474.html
標籤:
