【发布时间】:2018-09-24 15:40:55
【问题描述】:
所以这是我的问题,我相信在使用 PowerShell 和哈希表格式化输出时,您已经看过一千次了。所以我编写了一个脚本,它将从远程服务器(如 HD、MEM、CPU 等)收集所有必需品。首先我想说脚本工作正常,话虽如此,我在输出到 csv 的格式方面遇到问题.我正在寻找的是一些建议,如何帮助我将每个 HD 捕获到自己的列或分隔符中,以用于具有多个驱动器以及其他所有内容的系统。
所以这是我的功能齐全的脚本:
$OutFile = "$psscriptroot\sys_info.csv"
$Status = @()
$HDCAP = @()
$ArrComputers = "."
#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
$exp = @{Expression={$_.Server };Label="Computer_Name"},
@{Expression={$_.Manufacturer };Label="Manufacturer/Model"},
@{Expression={$_.Serial };Label="Serial #"},
@{Expression={$_.Processor };Label="Processor"},
@{Expression={$_.Harddrive};Label="Harddrive"},
@{Expression={$_.TotalMemorySize};Label="TotalMemorySize"},
@{Expression={$_.LastRebootTime};Label="LastRebootTime"}
foreach($sys in $computerSystem)
{
$mf = $sys | select Manufacturer,Model
}
foreach($cs in $computerSystem)
{
$mem = $cs | select -Property @{Name = "MemorySize GB" ; Expression = { $_.TotalPhysicalMemory/1GB –as [int] } }
}
foreach($CB in $computerBIOS)
{
$ser = $cb | select SerialNumber
}
foreach($CPU in $computerCPU)
{
$PRCR = $CPU | select name
}
foreach($HD in $computerHDD)
{
$HDCAP += $HD| select -Property DeviceID,
@{Name = "SizeGB" ; Expression = { $_.Size/1GB –as [int] } },
@{Name= "FreeGB"; Expression={[math]::Round($_.Freespace/1GB,2)}}
}
foreach($OS in $computerOS)
{
$CPOS = $os | select -Property @{Name = "LastBootUpTime " ; Expression = {$OS.ConvertToDateTime($OS.LastBootUpTime)}}
}
$Status += (@{
Server = $Computer
Manufacturer = $Mf
Serial = $ser
Processor = $PRCR
Harddrive = $HDCAP
TotalMemorySize = $mem
LastRebootTime = $CPOS
})
}
$Status | select-object $exp
这是我收到的输出副本:
Computer_Name : .
Manufacturer/Model : @{Manufacturer=Micro-Star International Co., Ltd; Model=MS-0000}
Serial # : @{SerialNumber=To be filled by O.E.M.}
Processor : @{name=AMD Ryzen 5 1600 Six-Core Processor }
Harddrive : {@{DeviceID=C:; SizeGB=447; FreeGB=158.99}, @{DeviceID=D:; SizeGB=931; FreeGB=473.66}, @{DeviceID=F:; SizeGB=0; FreeGB=0.11}}
TotalMemorySize : @{MemorySize GB=32}
LastRebootTime : @{LastBootUpTime =4/10/2018 9:54:16 AM}
所需的输出如下所示:
Computer_Name,Manufacturer/Model,Serial # ,Processor,Harddrive1, Harddrive 2, Harddrive 3 ,TotalMemorySize ,LastRebootTime
加上没有所有@ 符号。
请告诉我你的想法
【问题讨论】:
-
你的哈希表技术看起来很麻烦。你会更好地生成一些
PsCustomObjects。它们是组合此类数据的更好选择,并且更易于创建、管理和(特别与您的情况相关)导出到 csv。此外,请考虑使用Get-CimInstance而不是旧的 WMI cmdlet - 这些通常会更好地格式化输出,无需进行日期时间转换。 -
您好 Jose,感谢您对我的帖子进行编辑,我同意我的输出结果不适合这篇帖子。有了这个,我确实接受了你的建议,我必须说,一只鞋并不适合所有人。我确实喜欢关于使用 PScustomObject 构建哈希表的建议,但至于使用 Ciminstance,这并不总是给我想要的结果。如果你能提供一些有助于我的代码的想法,我会说我全神贯注。
标签: powershell output hashtable export-to-csv