0

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. 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.

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. Thanks in advance.

2 Answers2

0

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
js2010
  • 23,033
  • 6
  • 64
  • 66
0

Manually rebuilding the counters is the best way to fix this issue. Thank you @vonPryz for pointing it out.