我正在為我在 powershell 中的實習制作一個簡單的腳本,它使用變數、IF...ELSE 陳述句以及 Get-Counter 等簡單的東西。
$CpuLoad = "\Processor(_Total)\% Processor Time"
$Threshold = 50
Get-Counter -Counter $CpuLoad -SampleInterval 1 -MaxSamples 10
IF($CpuLoad -gt $Threshold) {
Write-Host "CPU Utilizacija ir lielaka par 50 procentiem!"
} Else {
Write-Host "Viss ok!"
}
那是劇本。不管 CPU 使用率是多少,它總是說ELSE write-host 陳述句,而不是 IF write-host 陳述句。不用擔心。我不是想作弊或類似的事情;)。我簡直傻眼了,這么簡單的腳本怎么能這么容易破解!任何幫助表示贊賞!
uj5u.com熱心網友回復:
將字串"\Processor(_Total)\% Processor Time"與數字進行比較50沒有多大意義。
相反,用于Where-Object測驗回傳的任何計數器樣本是否Get-Counter超過閾值:
$CpuLoad = "\Processor(_Total)\% Processor Time"
$Threshold = 50
$samplesExceedingThreshold = Get-Counter -Counter $CpuLoad -SampleInterval 1 -MaxSamples 10 |ForEach-Object CounterSamples |Where-Object CookedValue -gt $threshold |Select-Object -First 1
if($samplesExcheedingThreshold){
Write-Host "CPU Utilizacija ir lielaka par 50 procentiem!"
} else {
Write-Host "Viss ok!"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464033.html
