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

PowerShell面向对象编程:多PC监控信息输出问题求助

多台PC系统监控脚本问题解决

需求说明

需要获取多台PC的CPU利用率、内存及HDD使用情况,输出格式为先显示PC名称,再依次列出各项监控信息。

当前问题

  • CPU脚本无法为每台计算机输出对应的PC名称
  • 尚未完成将CPU、内存、HDD信息整合为统一输出的环节

现有脚本问题分析

CPU脚本(存在问题)

$computers = Get-Content -Path ()

foreach ($computer in $computers)

$CPUAvg = (get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average)

Write-Output $CPUAvg
  • 缺少foreach循环的大括号,代码逻辑不完整
  • Get-Counter未指定目标计算机,默认仅监控本地
  • 未将计算机名称与CPU计算结果关联输出

HDD脚本(运行正常但需补充远程支持)

$computers = Get-Content -Path ()
Get-CimInstance Win32_LogicalDisk | where{$_.DriveType -eq '3'} `
| Select DeviceID, DriveType,VolumeName,
@{N='TotalSize(GB)';E={[Math]::Ceiling($_.Size/1GB)}}, @{N='FreeSize(GB)';E={[Math]::Ceiling($_.FreeSpace/1GB)}} |ft -AutoSize
  • 未指定目标计算机,默认仅获取本地磁盘信息,需补充-ComputerName $computer参数支持远程采集

修正后的整合脚本

以下脚本会遍历目标计算机列表,同时获取CPU、内存、HDD信息,并统一输出包含PC名称的结构化结果:

# 读取目标计算机列表文件路径(替换为你的实际文件路径)
$computers = Get-Content -Path "C:\your\path\computers.txt"

# 遍历每台计算机
foreach ($computer in $computers) {
    try {
        # 获取CPU平均利用率(5次采样,间隔1秒)
        $cpuSamples = Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 -ComputerName $computer -ErrorAction Stop
        $cpuAvg = [math]::Round(($cpuSamples.CounterSamples.CookedValue | Measure-Object -Average).Average, 2)

        # 获取内存使用信息
        $memory = Get-CimInstance Win32_OperatingSystem -ComputerName $computer -ErrorAction Stop
        $totalMemoryGB = [math]::Ceiling($memory.TotalVisibleMemorySize / 1MB)
        $freeMemoryGB = [math]::Ceiling($memory.FreePhysicalMemory / 1MB)
        $usedMemoryGB = $totalMemoryGB - $freeMemoryGB
        $memoryUsagePercent = [math]::Round(($usedMemoryGB / $totalMemoryGB) * 100, 2)

        # 获取HDD使用信息
        $disks = Get-CimInstance Win32_LogicalDisk -ComputerName $computer -Filter "DriveType=3" -ErrorAction Stop | 
            Select-Object DeviceID, VolumeName,
                @{N='TotalSize(GB)';E={[Math]::Ceiling($_.Size/1GB)}},
                @{N='FreeSize(GB)';E={[Math]::Ceiling($_.FreeSpace/1GB)}},
                @{N='UsedSize(GB)';E={[Math]::Ceiling(($_.Size - $_.FreeSpace)/1GB)}},
                @{N='UsagePercent';E={[Math]::Round((($_.Size - $_.FreeSpace)/$_.Size)*100, 2)}}

        # 输出PC名称及汇总信息
        Write-Host "`n=== 计算机: $computer ===" -ForegroundColor Cyan
        Write-Host "CPU平均利用率: $cpuAvg%"
        Write-Host "内存总容量: $totalMemoryGB GB | 已使用: $usedMemoryGB GB ($memoryUsagePercent%) | 剩余: $freeMemoryGB GB"
        
        # 输出磁盘详细信息
        Write-Host "`n磁盘使用情况:"
        $disks | Format-Table -AutoSize

    } catch {
        Write-Host "`n无法连接到计算机 $computer : $_" -ForegroundColor Red
    }
}

关键修正点

  • 为foreach循环添加大括号,确保逻辑块正确执行
  • 所有远程监控命令(Get-Counter、Get-CimInstance)添加-ComputerName $computer参数,指定目标计算机
  • 将计算机名称与各项监控结果关联,统一输出结构
  • 添加try/catch错误处理,捕获无法连接的计算机并给出提示
  • 补充内存监控逻辑,完善信息维度
  • 格式化输出结果,提升可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 08:45:49