Search code examples
powershellservercpu-usagetaskschedulerperfmon

How to get Email alerts when CPU average utilization is grater than 90 percent for 30 min?


I am trying to get CPU utilization average for 30 min. If it is greater 90 percent for 30 min I can get email alerts using task scheduler and performance monitor and PowerShell script.

I have tried but getting Total CPU utilization after every 30 min.


Solution

  • The below script will give you the average processor time over 30 minutes. You can then have the script send an email if the processor average of the 30 minutes period is over 90. I commented the code for easier understanding.

    Resizable array you can add items to
    [System.Collections.ArrayList]$List = @()
    
    #Counter which is increased by 1 after getting each counter
    $Counter = 0
    
    #As long as the counter is less than the below value... (1800 = 30 minutes)
    While ($Counter -lt 10)
    {
        #Get 1 counter per second
        $CpuTime = $(Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 1).CounterSamples.CookedValue
    
        #Add the CPU total time to the list
        $List.Add($CpuTime)
    
        #Increase the counter by 1. As we are getting one sample per second, this will be increased by one each second
        $Counter++
    }
    
    #Get the minimum, average and maximum of the values stored in the list
    $Measurements = $List | Measure-Object -Minimum -Average -Maximum
    
    #Email bit. To befinished by you
    If ($Measurements.Average -gt 90)
    {
        Send-MailMessage ......
    }
    

    Hope this helps.