bigbignoob
bigbignoob

Reputation: 13

Kill PID exceeding threshold

I'm trying to write use a script as a workaround for a software bug for my org where the symptom is a specific PID will use endless threads. I want the script to kill a single PID (PID since there will be more than one with the process name) if it is using more than X threads. I'm testing with Chrome since it is easy to spawn multiple processes. I've been able to piece together a script for listing processes and thread usage but not sure how to then pipe/feed that into stopping the process. eg:

Get-Process -processname chrome |
Select-Object ID, @{Name='ThreadCount';Expression ={$_.Threads.Count}} | 
foreach 
if ($_.ThreadCount -gt 30) {stop-process -Force}

Thanks in advance

Upvotes: 1

Views: 47

Answers (1)

mklement0
mklement0

Reputation: 437998

Use Where-Object to filter the processes of interest and pipe them to Stop-Process:

Get-Process chrome |
  Where-Object { $_.Threads.Count -ge 30 } |
    Stop-Process -Force

Upvotes: 2

Related Questions