powershell - 当 CPU 平均利用率超过 90% 持续 30 分钟时,如何获取电子邮件警报?
问题描述
我正在尝试获得 30 分钟的 CPU 利用率平均值。如果在 30 分钟内超过 90%,我可以使用任务计划程序和性能监视器以及 PowerShell 脚本来获取电子邮件警报。
我尝试过,但每 30 分钟后获得总 CPU 利用率。
解决方案
以下脚本将为您提供超过 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 ......
}
希望这可以帮助。