I'm trying to build a script that will stop a process when it's hung at 45% CPU usage or more. I'm getting the error mentioned in the title. Could someone please review my script and tell me what I'm doing wrong? Again, I'm very new to powershell scripting so please be patient with me. Here is the script:
$ProcessName = "dfileman"foreach ($proc in (Get-WmiObject Win32_Processor)){
if($proc.numberofcores -eq $null){
$cores++
}else{
$cores = $cores + $proc.numberofcores
}
}
$cpuusage = [Math]::round(((((Get-Counter "\Process($ProcessName)\% Processor Time" -MaxSamples 2).Countersamples) [0].CookedValue)/$cores),2)
if ($cpuusage -gt 45){stop-process ($ProcessName)}