Windows Powershell 和串联?
Windows Powershell and concatenation?
我正在考虑使用 ForEach-Object 将本地主机上所有磁盘的容量相加。我能够列出尺寸,但它们是分开列出的。如何合并多个磁盘的大小以显示为一个计算?
这是我目前的情况:
$drives = Get-WmiObject Win32_logicalDisk
$drivecap = ($drives | ForEach-Object {$_.Size/1GB})
"The Total Capacity of all fixed drives: $drivecap GB" |输出文件 -FilePath $myfile -append
我希望实现类似 "The Total Capacity of all fixed drives: 800.99000000001 GB"
的目标
以下代码将帮助您从本地主机获取总磁盘 space。
$drives = Get-WmiObject Win32_logicalDisk
$total = 0
foreach($drive in $drives) {
$total += $drive.Size/1GB
}
Write-Host "The Total Capacity of all fixed drives:"$total
你可以这样做:
$drivecap = ($drives | measure -Property Size -Sum | select -Expand Sum) / 1GB
另一种选择:
$drivecap = $drives |% {$x=0} {$x += $_.Size/1GB} {$x}
我正在考虑使用 ForEach-Object 将本地主机上所有磁盘的容量相加。我能够列出尺寸,但它们是分开列出的。如何合并多个磁盘的大小以显示为一个计算?
这是我目前的情况:
$drives = Get-WmiObject Win32_logicalDisk
$drivecap = ($drives | ForEach-Object {$_.Size/1GB})
"The Total Capacity of all fixed drives: $drivecap GB" |输出文件 -FilePath $myfile -append
我希望实现类似 "The Total Capacity of all fixed drives: 800.99000000001 GB"
的目标以下代码将帮助您从本地主机获取总磁盘 space。
$drives = Get-WmiObject Win32_logicalDisk
$total = 0
foreach($drive in $drives) {
$total += $drive.Size/1GB
}
Write-Host "The Total Capacity of all fixed drives:"$total
你可以这样做:
$drivecap = ($drives | measure -Property Size -Sum | select -Expand Sum) / 1GB
另一种选择:
$drivecap = $drives |% {$x=0} {$x += $_.Size/1GB} {$x}