Last active
August 22, 2023 03:49
-
-
Save Badgerati/fcc89d73b9546f5c675adaee209581a3 to your computer and use it in GitHub Desktop.
Stops idle/crashed processes by Process Name via PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Stops idle/crashed processes by Process Name | |
.DESCRIPTION | |
This function will stop and idle or crashed processed with the passed ProcessName. | |
To detect idle processes, it will first gather an initial list of processes, sleep | |
for a period of time then refetch the list. If any of the new list are identical | |
to the initial list, then these processes are stopped as they're deemed idle. | |
WARNING: A process that is running but just left in the background is deemed "idle", | |
so will be killed by this function. This script is to on be used for very specific | |
processes that are known to run and then just stop idlely or crash. | |
.PARAMETER ProcessName | |
Specifies the name of the processes to find and stop | |
.PARAMETER SleepTime | |
Specifies a period of time, in seconds, for which to sleep. Default is 10 seconds | |
.EXAMPLE | |
PS> Stop-IdleProcess -ProcessName 'nunit-agent' | |
#> | |
function Stop-IdleProcess | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$ProcessName, | |
[int] | |
$SleepTime = 10 | |
) | |
# Get the list of initial processes | |
$initialProcesses = (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue | Select-Object NPM, CPU, Id) | |
# If there are no processes, return | |
if (($initialProcesses | Measure-Object).Count -eq 0) | |
{ | |
Write-Host "No processes found for $ProcessName" | |
return | |
} | |
# Sleep for a period of time | |
Start-Sleep -Seconds $SleepTime | |
# Now refetch the list of processes | |
$newProcesses = (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue | Select-Object NPM, CPU, Id) | |
# If there are no processes, then they've all finished so none idle | |
if (($newProcesses | Measure-Object).Count -eq 0) | |
{ | |
Write-Host "All $ProcessName processes have finished" | |
return | |
} | |
# Otherwise, we need to find process with the same ID/NPM and CPU, as these are idle/crashed | |
foreach ($new in $newProcesses) | |
{ | |
# Find the process from the initial ones | |
$match = $initialProcesses | Where-Object { $_.NPM -eq $new.NPM -and $_.CPU -eq $new.CPU -and $_.Id -eq $new.Id } | Select-Object -First 1 | |
# If there isn't one, continue | |
if ($match -eq $null) | |
{ | |
continue | |
} | |
# Otherwise, stop the process as it's crashed | |
Write-Host "Stopping $ProcessName process with ID: $($match.Id)" | |
Stop-Process -Id $match.Id -Force | Out-Null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment