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

使用Get-Counter获取部分服务器平均CPU使用率失败求助

问题描述
  1. 使用Get-Counter获取CPU平均使用率报错
    执行命令:
(Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue

报错信息:

Get-Counter: The specified object was not found on the computer.
  1. 使用Get-WmiObject获取的CPU使用率始终为0,与任务管理器显示不符
    执行命令:
Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Ave

输出始终为0,但任务管理器显示CPU使用率不为0。

  1. 尝试通过注册表排查时操作错误
    直接输入注册表路径:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\%servicename%\Performance

报错信息:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\%servicename%\Performance: The module 'HKEY_LOCAL_MACHINE' could not be loaded. For more information, run 'Import-Module HKEY_LOCAL_MACHINE'.

执行Import-Module HKEY_LOCAL_MACHINE同样报错。

解决方案

一、修复Get-Counter报错问题

  • 重建性能计数器,解决计数器损坏问题:
lodctr /R

执行完成后重启PowerShell,再尝试原命令。

  • 若仍报错,可能是系统语言导致的计数器路径不匹配,尝试对应语言的路径(比如中文系统):
(Get-Counter '\处理器(_总计)\% 处理器时间').CounterSamples.CookedValue
  • 先列出所有可用的处理器计数器,确认正确路径:
Get-Counter -ListSet Processor | Select-Object -ExpandProperty CounterNames

二、解决Get-WmiObject输出0的问题

  • Win32_Processor的LoadPercentage是单处理器即时负载,且WMI存在缓存问题,改用Get-CimInstance(替代Get-WmiObject的新cmdlet)并多次采样取平均:
$samples = @()
for ($i=0; $i -lt 5; $i++) {
    $samples += (Get-CimInstance Win32_Processor).LoadPercentage
    Start-Sleep -Milliseconds 200
}
($samples | Measure-Object -Average).Average
  • 也可以用Get-Counter多采样方式获取更准确的平均值:
(Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 1 -MaxSamples 5).CounterSamples | Measure-Object -Property CookedValue -Average | Select-Object -ExpandProperty Average

三、正确操作注册表排查的方法

  • PowerShell中不能直接输入注册表路径作为命令,需用Get-Item/Get-ChildItem操作,比如查看处理器性能计数器的注册表项:
Get-Item 'HKLM:\System\CurrentControlSet\Services\PerfProc\Performance'
  • 查看所有带性能计数器配置的服务项:
Get-ChildItem 'HKLM:\System\CurrentControlSet\Services' | Where-Object { Test-Path "$($_.PSPath)\Performance" }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 05:12:08