当CPU平均利用率在30分钟内达到90%以上时,如何获取电子邮件警报?

时间:2019-04-02 07:21:08

标签: powershell server cpu-usage taskscheduler perfmon

我正在尝试获取30分钟的CPU利用率平均值。如果30分钟大于90%,我可以使用任务计划程序,性能监视器和PowerShell脚本获得电子邮件警报。

我尝试过,但是每30分钟获得一次CPU总使用率。

1 个答案:

答案 0 :(得分:0)

以下脚本将为您提供30分钟以上的平均处理器时间。如果30分钟周期的处理器平均时间超过90,则可以让脚本发送电子邮件。 我对代码进行了注释,以便于理解。

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

希望这会有所帮助。