如何获得以GB代替B的输出

时间:2019-09-06 10:27:25

标签: powershell

我找到了一个脚本来检查远程计算机上的可用空间。但是我找不到如何获得GB而不是B的输出。 我对Powershell不太了解。有人可以帮我调整此代码,以便我可以查看远程计算机上的可用磁盘空间

getFilteredRows

2 个答案:

答案 0 :(得分:2)

实现此目标的一种可能方法是使用以下代码:

gwmi win32_logicaldisk -ComputerName STR3598C -Filter "DeviceID='C:'" | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

预期输出:

DeviceId MediaType   Size FreeSpace
-------- ---------   ---- ---------
C:              12 215.52     111.1

答案 1 :(得分:1)

另一种方法是将结果存储在变量中(如您的代码示例所示),并将其格式化以输出到屏幕和/或将其保存在CSV文件中:

$computer = 'STR3598C'
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter "DeviceID='C:'" | 
        Select-Object @{Name = 'Computer'; Expression = {$_.PSComputerName}},
                      DeviceID, VolumeName,
                      @{Name = 'Size'; Expression = {'{0:N2}' -f ($_.Size / 1GB)}},
                      @{Name = 'FreeSpace'; Expression = {'{0:N2} GB' -f ($_.FreeSpace / 1GB)}}

# output on screen
$disk | Format-Table -AutoSize

# or save the output as CVS
$disk | Export-Csv -Path 'D:\disksize.csv' -NoTypeInformation

屏幕输出

Computer DeviceID VolumeName Size      FreeSpace
-------- -------- ---------- ----      ---------
STR3598C C:       Systeem    232,79 GB 89,33 GB