powershell looping problem

hi guys,

i have a script that works but i have an issue with the final else

what i want is for the final else (line 20) to repeat the script from line 1

any ideas? i can’t seem to get it (might be because the kids have worn me out today:o:o


$repeat_count = 5
$cpu_threshold = 85
$sleep_interval = 5
$hit = 0
foreach($turn in 1..$repeat_count) {
$cpu = (gwmi -class Win32_Processor).LoadPercentage
write-host “CPU utilization is Currently at $cpu`%”
If($cpu -gt $cpu_threshold) {
$hit = $hit+1
}
start-sleep $sleep_interval
}

if($hit -eq 5) {
write-host “CPU utilization is over threshold”


$A = Start-Process -FilePath C:\alerts\emailalert.bat -Wait -passthru -WindowStyle Hidden;$a.ExitCode 
} else {
write-host “CPU utilization is below threshold level”

}

I’ve just run it in Windows 7 on my lappy and it did the 5 hits and then fell over

The next time I ran it, it did more than 5 hits before it fell over. Not sure what output you were expecting, but it definitely is hitting a snag at the else point. Other than that, the system is reporting when below threshhold, but falls over when above. Do you have to specify an upper and lower limit?


Not to up on shell, but if you need anyone to test, let me know.

[QUOTE=Droid;466093]I’ve just run it in Windows 7 on my lappy and it did the 5 hits and then fell over

The next time I ran it, it did more than 5 hits before it fell over. Not sure what output you were expecting, but it definitely is hitting a snag at the else point. Other than that, the system is reporting when below threshhold, but falls over when above. Do you have to specify an upper and lower limit?


Not to up on shell, but if you need anyone to test, let me know.[/QUOTE]

hi Droid,

the error your getting is due to the threshold for cpu not being hit. if copy that script into notepad and save as a *.ps1 file and run it you will find it behaves better…

to test the over threshold change line 2 $cpu_threshold = 85 to $cpu_threshold = 1

you will get an error at the point it calls the batch file but thats to be expected as you don’t have the batch file.

if you comment out the line

$A = Start-Process -FilePath C:\alerts\emailalert.bat -Wait -passthru -WindowStyle Hidden;$a.ExitCode

then you shouldn’t see any errors…

i appreciate you taking a look.

i think i need to turn my script into a function then just have it repeat the function till the cpu threshold is reached…

Do you only want it as a continuous monitor and only to report when over the threshold and send the e-mail alert, if so, do you need the Else part anyway? If it’s all below the CPU load you don’t need the system to report anything and you can just it leave it running and only stop when the CPU goes over the threshold.

Am I reading this right?

i will give that a go. the reason i needed it was something was causing the explorer.exe process to use 100% cpu and i couldn’t identify what it was exactly. I was looking at this last night and it turned out to be my antivirus so a few carefully placed exceptions and that should be sorted now.

thanks for the help :slight_smile: