出于安全要求,我使用Get-WmiObject cmdlet来监控未安装PowerShell的服务器的平均CPU使用率。
$CPU = Get-WmiObject Win32_Processor -computername $computerName | Measure-Object -property LoadPercentage -Average | Select Average
$CPULoad = $($CPU.average)
if ( $CPULoad -ge $ThresholdCPU ){
Write-output "High CPU usage: $CPULoad % on $computerName"
}
Else {
Write-output "CPU usage on $computerName is normal: $CPULoad %"
}当当前CPU使用率高于手动设置的CPU阈值iv'e时,我的脚本工作正常。
但是,由于远程服务器中的CPU使用率激增,我面临着许多错误警报。
在阅读了cmdlet的文档后,我发现与Get-Counter cmdlet相反,Get-WmiObject没有某种SampleInterval属性。
有没有办法使用Get-WmiObject来实现这一点,所以if标准只有在3个有效样本之后才为真?
发布于 2019-04-30 22:10:17
也许使用固定次数的循环可以达到您想要的效果:
$maxAttempts = 3
for ($attempt = 0; $attempt -lt $maxAttempts; $attempt++) {
$CPULoad = (Get-WmiObject Win32_Processor -ComputerName $computerName |
Measure-Object -property LoadPercentage -Average).Average
if ( $CPULoad -le $ThresholdCPU ) { break }
# do nothing for x seconds and test CPU load again
Start-Sleep -Seconds 1
}
if ($attempt -lt $maxAttempts) {
Write-output "CPU usage on $computerName is normal: $CPULoad %"
}
else {
Write-output "High CPU usage: $CPULoad % on $computerName"
}https://stackoverflow.com/questions/55891533
复制相似问题