如何在没有 属性 字段的情况下仅获取设备名称和免费 space 百分比?
How to get only the device name and the free space percentage WITHOUT the property field?
我有一个命令 returns DeviceID 和免费 space 百分比如下:
$devices= Get-CimInstance -Class Win32_LogicalDisk -Filter "Drivetype=3" | Select-Object DeviceID, @{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
Write-Output $devices
输出不错,如下:
DeviceID : C:
Free (%) : 33 %
PSComputerName : MIKAELCOMP
RunspaceId : c2b02166-4e08-4e90-b8f5-5defafa20ac9
DeviceID : D:
Free (%) : 22 %
PSComputerName : MIKAELCOMP
RunspaceId : c2b02166-4e08-4e90-b8f5-5defafa20ac9
但是,我需要这样的输出:
C: 33%
D: 22%
如您所见,没有第一列中的描述。
有什么办法吗?
(另外,我不知道为什么我会得到最后两个字段:PSComputerName 和 运行spaceId)。
谢谢。
您可以为此使用 string format operator (-f
):
$devices | ForEach-Object {
"{0} {1}" -f $_.DeviceID,$_.'Free (%)'.Trim()
}
Trim()
用于删除您的百分比值可能具有的前后空格。 TrimStart()
可用于仅删除前面的空格。 TrimEnd()
可用于仅删除尾随空格。
我有一个命令 returns DeviceID 和免费 space 百分比如下:
$devices= Get-CimInstance -Class Win32_LogicalDisk -Filter "Drivetype=3" | Select-Object DeviceID, @{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
Write-Output $devices
输出不错,如下:
DeviceID : C:
Free (%) : 33 %
PSComputerName : MIKAELCOMP
RunspaceId : c2b02166-4e08-4e90-b8f5-5defafa20ac9
DeviceID : D:
Free (%) : 22 %
PSComputerName : MIKAELCOMP
RunspaceId : c2b02166-4e08-4e90-b8f5-5defafa20ac9
但是,我需要这样的输出:
C: 33%
D: 22%
如您所见,没有第一列中的描述。
有什么办法吗?
(另外,我不知道为什么我会得到最后两个字段:PSComputerName 和 运行spaceId)。
谢谢。
您可以为此使用 string format operator (-f
):
$devices | ForEach-Object {
"{0} {1}" -f $_.DeviceID,$_.'Free (%)'.Trim()
}
Trim()
用于删除您的百分比值可能具有的前后空格。 TrimStart()
可用于仅删除前面的空格。 TrimEnd()
可用于仅删除尾随空格。