问题描述
我有不错的PowerShell代码,可以从列表中远程获取序列号和型号(以及其他详细信息),并将其导出到PowerShell中的漂亮视图中。
但是,我想将其导出到一个已整理的csv文件中,所以请不要使用它:
我要的是
这是原始代码:
$ArrComputers = "yakovcomputer"
#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
write-host "System information for: " $computerSystem.Name -BackgroundColor DarkCyan
"-------------------------------------------------------"
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"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 + ",Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"-------------------------------------------------------"
}
所以我尝试在开始处添加Import-Csv C:list.txt | ForEach-Object
(其中具有 yakovcomputer 名称)
(未决定是否使用$computers = Get-Content c:\list.txt
)
并在末尾添加export-csv c:\temp\list.csv
命令。
所以现在看起来像这样(编辑后的代码):
Import-Csv C:list.csv | ForEach-Object
{
$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
}
export-csv c:\temp\list.csv
但是它一直都在出错。
我知道我在这里写错了(代码),但是,实现该目标的正确方法是什么?
解决方法
如果您的list.txt
只是计算机名称的纯文本文件,每行只有一个名称,那么Get-Content
就足够了。您可以使用$_
或$PSItem
访问当前行(当前项目)。
Export-Csv
需要个对象。因此,您必须将数据转换为对象。有几种方法可以实现,其中大多数方法需要创建一个HashTable。这是我的首选版本:
Get-Content c:\list.txt | foreach
{
$computerSystem = Get-WmiObject Win32_ComputerSystem -Computer $_
$computerBIOS = Get-WmiObject Win32_BIOS -Computer $_
$computerOS = Get-WmiObject Win32_OperatingSystem -Computer $_
$computerCPU = Get-WmiObject Win32_Processor -Computer $_
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter drivetype=3
New-Object PSObject -Property @{
"Manufacturer" = $computerSystem.Manufacturer
"Model" = $computerSystem.Model
"Serial Number" = $computerBIOS.SerialNumber
"CPU" = $computerCPU.Name
"HDD Capacity" = ("{0:N2}" -f ($($computerHDD|measure Size -sum).Sum/1GB) + "GB")
"HDD Space" = ("{0:P2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/$($computerHDD|measure Size -sum).Sum) + " Free (" + "{0:N2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/1GB) + "GB)")
"RAM" = ("{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB")
"Operating System" = ($computerOS.caption + ",Service Pack: " + $computerOS.ServicePackMajorVersion)
"User logged In" = $computerSystem.UserName
"Last Reboot" = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
}
} | Export-Csv c:\temp\list.csv -NoTypeInformation
请注意$($computerHDD|measure Size -sum).Sum
构造,如果有多个HDD,则将它们的值相加。
另外,请考虑改用CIM instead of WMI。