【问题标题】:PowerShell local disk property no outputPowerShell本地磁盘属性无输出
【发布时间】:2013-12-12 11:39:07
【问题描述】:

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

$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

【问题讨论】:

    标签: properties powershell-3.0 psobject


    【解决方案1】:

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

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

    $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
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2019-06-09
      • 2011-01-21
      • 2020-08-10
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多