简体   繁体   English

如何在不使用性能计数器对象的情况下获得Powershell中的CPU百分比?

[英]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.

相关问题 如何在 powershell 中获取没有实例名称的总 CPU 使用率百分比 - How to get Total CPU usage percentage without instance name in powershell Powershell获取CPU百分比 - Powershell Get CPU Percentage 获取与Powershell计数器一起使用的CPU的百分比 - getting percentage of cpu used with powershell counter 通过Powershell获得出色的性能 - Get counter performance with powershell 如何使用Powershell命令确定平均CPU百分比并以无标签的数字百分比形式输出CPU百分比 - How to Determine the Average CPU Percentage using a Powershell Command and Output the CPU Percentage as a Numeric Percentage with no Labels 是否可以使用 Powershell 命令 get-counter 获取性能计数器描述? - Is it possible to get performance counter description using Powershell command get-counter? 我们如何使用过去 7 天的 powershell 在 excel 中获取 azure sql 数据库指标报告(cpu 百分比、dtu 百分比、死锁、连接失败) - How can we get azure sql database metrics report in excel (cpu percentage,dtu percentage,deadlocks,failed connection) using powershell of last 7 days Powershell 中的 CPU 使用百分比 - CPU usage in percentage in Powershell 使用 PowerShell 获取 CPU 百分比 - getting CPU percentage with PowerShell msmq队列性能计数器的Powershell get-counter不起作用 - powershell get-counter for msmq queue performance counter not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM