我正在研究基于http://www.powershellneedfulthings.com/?p=36的电源shell脚本,以检查没有分配驱动器号的卷的磁盘空间。
该脚本运行良好,但我想过滤只显示可用磁盘空间少于10%的驱动器。 我正在使用带有哈希表的where-object过滤器遇到麻烦。
# calculations for displaying disk size information $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1GB),2)}} $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1GB),2)}} $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1GB)/($_.Capacity / 1073741824)) * 100),0)}} # array declarations $volumes = @() # import server names to check $servers = (Get-Content .\servers.txt) # check disk space for volumes without drive letter foreach ($server in $servers){ $volumes += Get-WmiObject -computer $server win32_volume | Where-Object {$_.DriveLetter -eq $null -and $_.Label -ne "System Reserved"} } $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize我尝试的是:
Where-Object {$FreePerc -le 10}目前的输出是:
SystemName Label Capacity(GB) FreeSpace(GB) Free(%) ---------- ----- ------------ ------------- ---- SERVER01 X:\data\ 9.97 0.89 9 SERVER01 X:\log\ 9.97 1.20 12 SERVER01 X:\info\ 9.97 3.49 35我只想显示可用磁盘空间少于10%的卷。 所以在这种情况下,只应显示第一个条目。
谢谢!
I am working on a power shell script based on http://www.powershellneedfulthings.com/?p=36 to check the disk space for volumes that do not have a driver letter assigned.
The script works pretty well, but I'd like to filter that only drives are shown that have less than 10% free disk space. I'm running into troubles using the where-object filter with hash tables.
# calculations for displaying disk size information $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1GB),2)}} $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1GB),2)}} $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1GB)/($_.Capacity / 1073741824)) * 100),0)}} # array declarations $volumes = @() # import server names to check $servers = (Get-Content .\servers.txt) # check disk space for volumes without drive letter foreach ($server in $servers){ $volumes += Get-WmiObject -computer $server win32_volume | Where-Object {$_.DriveLetter -eq $null -and $_.Label -ne "System Reserved"} } $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSizeWhat I tried is:
Where-Object {$FreePerc -le 10}The current output is:
SystemName Label Capacity(GB) FreeSpace(GB) Free(%) ---------- ----- ------------ ------------- ---- SERVER01 X:\data\ 9.97 0.89 9 SERVER01 X:\log\ 9.97 1.20 12 SERVER01 X:\info\ 9.97 3.49 35I'd like to only show the volumes that have less than 10% free disk space. So in this case, only the first entry should be shown.
Thanks!
最满意答案
我认为where子句变量$FreePerc是个问题。 Arco有正确的想法。
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.'Free(%)' -le 10} | Format-Table -AutoSize我把属性放在单引号中,因为我认为PowerShell会尝试评估(%) 。 同样为了使Arco的解决方案工作,可能更容易调用$FreePerc的Name $FreePerc 。 这样你只需要更新一个位置
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.($FreePerc.Name) -le 10} | Format-Table -AutoSizeI think the where clause variable $FreePerc is the issue. Arco had the right idea.
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.'Free(%)' -le 10} | Format-Table -AutoSizeI put the property in single quotes because i think PowerShell would try to evaluate (%) otherwise. Also to make Arco's solution work it might just be easier to call the Name propery of $FreePerc. That way you only have to update one location
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.($FreePerc.Name) -le 10} | Format-Table -AutoSize更多推荐
发布评论