[英]How to get CPU percentage in powershell without using performance counter objects?
I'm trying to get CPU Percentage from multiple computers in my organization and some of these computers due to registry failure or corruption, their performance counters doesn't return any data. 我正在尝试从组织中的多台计算机以及其中某些计算机由于注册表故障或损坏而获得CPU百分比,它们的性能计数器不会返回任何数据。 I've been trying to find a code to find the CPU percentage without using these counters so that my powershell script will work on these computers as well.
我一直在尝试查找无需使用这些计数器即可找到CPU百分比的代码,以便我的Powershell脚本也可以在这些计算机上工作。
I realized that this could happen when I started pushing the below code: 我意识到,当我开始推送以下代码时,可能会发生这种情况:
$ $($(Get-WmiObject win32_processor | select LoadPercentage).LoadPercentage) | Measure-Object -Average).Average
after which I tried the Common Information Module (codes below) : 之后,我尝试了公共信息模块(下面的代码):
((GET-CIMInstance -class Win32_PerfFormattedData_Counters_ProcessorInformation) | Measure-Object -property PercentProcessorPerformance -Average)
(Get-CimInstance -ClassName Win32_Processor).LoadPercentage
If there's any suggestions here or a piece of code which can help me get the CPU percentage without using the counter, please advice. 如果此处有任何建议或一段代码可以帮助我在不使用计数器的情况下获得CPU百分比,请提出建议。 Thanks in advance.
提前致谢。
I've done it this way. 我是这样做的。
$sleepseconds = 1
$numcores = 4
$id = 6416
$cpu1 = (get-process -Id $id).cpu
sleep $sleepseconds
$cpu2 = (get-process -Id $id).cpu
$cpupercent = [int](($cpu2 - $cpu1)/($numcores*$sleepseconds) * 100)
$cpupercent
Manually rebuilding the counters is the best way to fix this issue. 手动重建 计数器是解决此问题的最佳方法。 Thank you @vonPryz for pointing it out.
谢谢@vonPryz指出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.