我想在我的powershell脚本中添加Used space列?

时间:2017-03-23 07:52:41

标签: powershell

我想对下面的脚本进行一些更改。

 try{
$space = Get-WmiObject Win32_logicaldisk `
| Format-Table DeviceID,`
 @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
 @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
@{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
-AutoSize
}
catch 
{
echo "Exception Occurred. Please try again on $servername"
}

echo "The total space on $servername are given below:"
echo $space

我想添加一个显示已用空间的列,而不对输出格式进行任何更改。

输出如下:

The total space on  are given below:

DeviceID Size(GB) Free Space(GB) Free (%)
-------- -------- -------------- --------
A:              0              0         
C:             60             41   69 %  
D:            100             78   78 %  
E:            200            190   95 %  
G:              0              0   

2 个答案:

答案 0 :(得分:2)

您可以按如下方式扩展表格

try
{
    $space = Get-WmiObject Win32_logicaldisk | Format-Table DeviceID,
    @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},
    @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},
    @{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}},
    @{Name="Used Space(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb - $_.freespace/1gb))}},
    @{Name="Used Space (%)";Expression={"{0,6:P0}" -f(($_.size/1gb - $_.freespace/1gb) / ($_.size/1gb))}}
    -AutoSize
}
catch 
{
    echo "Exception Occurred. Please try again on $servername"
}

echo "The total space on $servername are given below:"
echo $space

答案 1 :(得分:0)

我自己找到了答案,请参考下面的代码:

$servername = hostname

#checking for disk space
try{
$space = Get-WmiObject Win32_logicaldisk `
| Format-Table DeviceID,`
 @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
 @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
 @{Name="Used Space(GB)";Expression={[decimal]("{0:N0}" -f(($_.size/1gb) - ($_.freespace/1gb)))}}, `
@{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
-AutoSize
}
catch 
{
echo "Exception Occurred. Please try again on $servername"
}

echo "The total space on $servername are given below:"
echo $space 

所需的结果是:

The total space on sw02014 are given below:

DeviceID Size(GB) Free Space(GB) Used Space(GB) Free (%)
-------- -------- -------------- -------------- --------
A:              0              0              0         
C:             60             41             18   69 %  
D:            100             78             22   78 %  
E:            200            190             10   95 %  
G:              0              0              0