0

我正在为磁盘构建报告,但需要帮助,请。这是代码:

$outputs = @()
$disks = get-wmiobject -class "Win32_LogicalDisk" -Filter 'driveType=3' -namespace "root\CIMV2"
foreach ( $disk in $disks ) {
    $output = New-Object PSObject -Property @{
        deviceID     = $disk.caption
        FileSystem   = $disk.fileSystem
        FreeSpace    = $disk.freeSpace/1GB
        Size         = $disk.size/1GB
        VolumeName   = $disk.volumeName

        used = "{0:N1}" -f ((("{0:N1}" -f ($disk.size/1gb)).ToString().replace(",",".") - ("{0:N1}" -f ($disk.freespace/1gb)).ToString().replace(",","."))   /   ("{0:N1}" -f ($disk.size/1gb)).ToString().replace(",",".") * 100)

        used2 = $used
    }
    $outputs += $output
}
$outputs | select deviceID, VolumeName, size, used, used2  | Format-Table

一切都很好,但变量 used2 中没有

deviceID VolumeName             Size used used2
-------- ----------             ---- ---- -----
C:       win        49,1992149353027 87,4
D:       data       170,701168060303 70,7
4

1 回答 1

0

$used在该范围内不作为变量存在,因此used2被设置为$null.

您需要在调用$used 之前New-Object创建一个变量。此外,您在计算已用百分比时使用了太多格式 - 将格式保存到最后。下面,我进行了两项更改

$used = "{0:P1}" -f (($disk.size - $disk.FreeSpace) / $disk.size);
$output = New-Object PSObject -Property @{
        deviceID     = $disk.caption
        FileSystem   = $disk.fileSystem
        FreeSpace    = $disk.freeSpace/1GB
        Size         = $disk.size/1GB
        VolumeName   = $disk.volumeName
        used = $used
        used2 = $used
        }
于 2013-12-12T20:04:26.397 回答