locked
Powershell CPU usage script RRS feed

  • Question

  • Hi people,

    I am quite new to PowerShell, I am trying make a script to check out the CPU usage, however when I run my script in PowerShell it always seem to spit out the same failing result. even when I put the threshold on 1% or 80% it seems to make no diffrence.

    This is what I have:

          Try {
    
        $Threshold = 80
    
        $CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average )
    
        Write-Host "CPU load is" $CpuLoad "%"
    
        IF($CpuLoad -gt $Threshold) {
    
            Write-Host "CPU load greater than !" $Threshold "%"
    
            Write-Host  "Script Check Failed"
    
        } Else {      
            Write-Host  "Script Check Passed"
            }
        } Catch {
    
        Write-Host  "Script Check Failed"
       
        }

    Result:

    CPU load is @{Average=10} %
    Script Check Failed



    So when the CPU Usage is greater then 80% it should say: "Script check Failed" and when it is below it should pass. but thats obviously not working.

    Feedback is greatly appreciated, thanks in advance :)
    Lars B.


    Wednesday, February 28, 2018 3:31 PM

Answers

  • Hi,

    You almost did it!

    There is just a .Average missing at the end of your $CpuLoad = ...

    $CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average

    • Marked as answer by Lactar Wednesday, February 28, 2018 3:38 PM
    Wednesday, February 28, 2018 3:35 PM
  • The following will get you the system generated total for all processors.  This is not an average but is a correctly computed usage number for the number of processors.

    Get-Counter '\Processor(*)\% Processor Time' -Continuous |
        select -expand CounterSamples | 
        where{$_.InstanceName -eq '_total'}

    You can just extend the pipeline for continuous monitoring.

    Get-Counter '\Processor(*)\% Processor Time' -Continuous |
        select -expand CounterSamples | 
        where{$_.InstanceName -eq '_total' -and $_.CookedValue -gt 40} |
        ForEach{Write-Host $_.CookedValue -fore Red}


    \_(ツ)_/

    • Marked as answer by Lactar Wednesday, February 28, 2018 4:05 PM
    Wednesday, February 28, 2018 4:01 PM

All replies

  • Hi,

    You almost did it!

    There is just a .Average missing at the end of your $CpuLoad = ...

    $CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average

    • Marked as answer by Lactar Wednesday, February 28, 2018 3:38 PM
    Wednesday, February 28, 2018 3:35 PM
  • It does work now! Thanks alot Toby, you have solved my biggest mystery in litterally no-time! :)
    Wednesday, February 28, 2018 3:41 PM
  • The following is faster and much more accurate. 

    Get-Counter '\Processor(*)\% Processor Time' | select -expand CounterSamples

    You cannot average across processors.  You can only report on individual processor % usage.  You can average reading over time per processor.

    There are some good articles on MSDN discussing how to measure performance correctly.


    \_(ツ)_/

    Wednesday, February 28, 2018 3:52 PM
  • The following will get you the system generated total for all processors.  This is not an average but is a correctly computed usage number for the number of processors.

    Get-Counter '\Processor(*)\% Processor Time' -Continuous |
        select -expand CounterSamples | 
        where{$_.InstanceName -eq '_total'}

    You can just extend the pipeline for continuous monitoring.

    Get-Counter '\Processor(*)\% Processor Time' -Continuous |
        select -expand CounterSamples | 
        where{$_.InstanceName -eq '_total' -and $_.CookedValue -gt 40} |
        ForEach{Write-Host $_.CookedValue -fore Red}


    \_(ツ)_/

    • Marked as answer by Lactar Wednesday, February 28, 2018 4:05 PM
    Wednesday, February 28, 2018 4:01 PM
  • Thanks! I will look into it and tweak my "code" tomorrow, I really appreciated your answers! :)
    Wednesday, February 28, 2018 4:07 PM
  • I also recommend increasing the sample interval to avoid short duration spikes.  Decide how long a time the exceeded total should exist for before it is considered and issue.

    Get-Counter '\Processor(*)\% Processor Time' -Continuous -SampleInterval 60

    The above would total the usage over 60 seconds and test/report the system computed total % over that time.


    \_(ツ)_/

    Wednesday, February 28, 2018 4:11 PM