powershell 将 WMI 查询结果合并为一个 table
powershell Combine WMI Query results into one table
我想从多台电脑上抓取磁盘信息并输出
这个有效:
foreach ($args in $file) {
get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $args -Filter "Drivetype=3" |
ft SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
}
但输出如下所示:
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer1 C: OS 100 31
Computer1 D: DATA 500 200
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer2 C: OS 110 48
Computer2 D: DATA 500 201
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer3 C: OS 100 38
Computer3 D: DATA 500 260
我希望将它们组合起来,这样输出如下所示:
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer1 C: OS 100 31
Computer1 D: DATA 500 200
Computer2 C: OS 110 48
Computer2 D: DATA 500 201
Computer3 C: OS 100 38
Computer3 D: DATA 500 260
我研究过创建一个新的 object,但我不确定如何在同一个 header 下的循环中将 WMI 查询结果添加到那个 object。
用 ForEach-Object
cmdlet 替换 foreach
循环并将 Format-Table
(ft
) 移动到管道的最后:
$file |ForEach-Object {
Get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $_ -Filter "Drivetype=3"
} |Format-Table SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
(另外,不要那样使用 $args
,它是一个 automatic variable)
如果您打算稍后在脚本中使用 WMI 的输出,请使用 Select-Object
而不是 Format-Table
。
然后在需要显示的时候使用Format-Table
:
$LogicalDisks = $file |ForEach-Object {
Get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $_ -Filter "Drivetype=3"
} |Select-Object SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }}
# Do some stuff with $LogicalDisks
# ...
# Time to display the disks to the user
$LogicalDisks |Format-Table -AutoSize
我想从多台电脑上抓取磁盘信息并输出
这个有效:
foreach ($args in $file) {
get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $args -Filter "Drivetype=3" |
ft SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
}
但输出如下所示:
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer1 C: OS 100 31
Computer1 D: DATA 500 200
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer2 C: OS 110 48
Computer2 D: DATA 500 201
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer3 C: OS 100 38
Computer3 D: DATA 500 260
我希望将它们组合起来,这样输出如下所示:
SystemName DeviceID VolumeName Total Size Free Size
---------- -------- ---------- ---------- ---------
Computer1 C: OS 100 31
Computer1 D: DATA 500 200
Computer2 C: OS 110 48
Computer2 D: DATA 500 201
Computer3 C: OS 100 38
Computer3 D: DATA 500 260
我研究过创建一个新的 object,但我不确定如何在同一个 header 下的循环中将 WMI 查询结果添加到那个 object。
用 ForEach-Object
cmdlet 替换 foreach
循环并将 Format-Table
(ft
) 移动到管道的最后:
$file |ForEach-Object {
Get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $_ -Filter "Drivetype=3"
} |Format-Table SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
(另外,不要那样使用 $args
,它是一个 automatic variable)
如果您打算稍后在脚本中使用 WMI 的输出,请使用 Select-Object
而不是 Format-Table
。
然后在需要显示的时候使用Format-Table
:
$LogicalDisks = $file |ForEach-Object {
Get-WmiObject win32_logicaldisk -Credential $cre -ComputerName $_ -Filter "Drivetype=3"
} |Select-Object SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }}
# Do some stuff with $LogicalDisks
# ...
# Time to display the disks to the user
$LogicalDisks |Format-Table -AutoSize