You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求筛选低剩余空间磁盘并显示主机名的PowerShell磁盘查询脚本

调整后满足需求的PowerShell脚本

针对你提出的两点调整要求,改动逻辑如下:

  • 新增PSComputerName字段直接读取WMI查询默认返回的主机名属性
  • 新增Where-Object过滤规则,判断剩余空间占比低于2%
Get-WmiObject Win32_LogicalDisk -ComputerName computer1, computer2, computer3 -Filter "DriveType=3" | 
Where-Object { ($_.FreeSpace / $_.Size) -lt 0.02 } |
Select-Object PSComputerName, DeviceID, 
@{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, 
@{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}},
@{'Name'='剩余占比'; 'Expression'={[string]::Format('{0:P2}', $_.FreeSpace / $_.Size)}}
通用查询磁盘剩余空间低于5%的PowerShell脚本

支持自定义主机列表、阈值,可选导出结果到文件:

# 可自定义配置项
$computerList = @("computer1", "computer2", "computer3") # 替换为实际要查询的主机列表
$threshold = 0.05 # 剩余空间阈值,5%对应取值为0.05
$outputPath = "C:\磁盘空间告警.csv" # 可选配置,导出结果的本地路径

# 执行查询逻辑
Get-WmiObject Win32_LogicalDisk -ComputerName $computerList -Filter "DriveType=3" -ErrorAction SilentlyContinue | 
Where-Object { $_.Size -gt 0 -and ($_.FreeSpace / $_.Size) -lt $threshold } |
Select-Object @{Name='主机名称';Expression={$_.PSComputerName}},
@{Name='盘符';Expression={$_.DeviceID}},
@{Name='总容量(GB)';Expression={[math]::Round($_.Size / 1GB, 2)}},
@{Name='剩余容量(GB)';Expression={[math]::Round($_.FreeSpace / 1GB, 2)}},
@{Name='剩余占比';Expression={[string]::Format('{0:P2}', $_.FreeSpace / $_.Size)}} |
Format-Table -AutoSize
# 如需要导出结果到文件,将上一行的Format-Table命令替换为:
# Export-Csv -Path $outputPath -Encoding UTF8 -NoTypeInformation

内容的提问来源于stack exchange,提问作者Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 03:48:03