Powershell可用磁盘空间,用于没有驱动器号的驱动器(Powershell free disk space for drives without drive letter)

编程入门 行业动态 更新时间:2024-10-28 07:23:05
Powershell可用磁盘空间,用于没有驱动器号的驱动器(Powershell free disk space for drives without drive letter)

我正在研究基于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 -AutoSize

What 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 35

I'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 -AutoSize

I 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 -AutoSize

I 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

更多推荐

本文发布于:2023-07-31 02:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动器   磁盘空间   free   Powershell   disk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!