获取磁盘信息并转换为GB

时间:2018-02-20 12:06:21

标签: powershell

我一直在研究这个脚本来捕获磁盘大小,但是我有它们以字节为单位并且它们很乱,我已经看了几种添加小数点的方法,但是我在转换时遇到了麻烦首先是GB,我还试图计算可用磁盘大小和使用的百分比。

$servers = (Get-AdComputer -Filter "name -like MY-VM-*'").Name | Sort-Object

foreach ($Server in $Servers)
{
$disks= get-wmiobject -ComputerName $Server Win32_volume | select-object pscomputername, Capacity, driveletter, freespace |`
 format-table | out-file c:\temp\new3.txt -append

Foreach ($disk in $disks)
{
  $driveletter = $disk.driveletter
  $totalsize = $disk.capacity
  $freespace = $disk.freespace
  $freespace = [math]::round($freespace[$_] /9.3132257461548)
  $Percent_Free = [math]::round(($freespace / $totalsize ) * 100)
   }  
}

希望有道理:)

1 个答案:

答案 0 :(得分:1)

您的查询已在评论中得到解答。你可以直接这样做 -

$servers = (Get-AdComputer -Filter "name -like MY-VM-*'").Name | Sort-Object

foreach ($Server in $Servers)
{
$disks= get-wmiobject -ComputerName $Server Win32_volume | select-object pscomputername, Capacity, driveletter, freespace |`
 format-table | out-file c:\temp\new3.txt -append

Foreach ($disk in $disks)
{
  $driveletter = $disk.driveletter
  $totalsize = $disk.capacity/1GB
  $freespace = $disk.freespace/1GB
  $Percent_Free = [math]::round(($freespace / $totalsize ) * 100)
   }  
}