Killing a stalled process and leaving others running using PowerShell

Since I wrote my post “Using Loadrunner / Performance Center to kill a rogue process“, some colleagues have asked if I could kill a stalled process and leave others up and running. After some “Googling”, I have found that this is possible.

I modified some PowerShell scripts that I found online so that they do what we want. If you paste the code below into a PowerShell script called for example “KillStalledProcesses.ps1”, it will scan through all the processes on a machine and “kill” any that are not responding.

I’ve tested it using SysInternals “Process Explorer” which allows you to “suspend” a running process like in the image below. I suspended the “notepad.exe” process and tested the PowerShell code.

The code correctly identified the suspended process and “killed” it.

(When I tested this with two other running “notepad” processes, it left the two other processes running.)

My thoughts are that we could add the PowerShell script into the “Extra Files” portion of a LoadRunner or Performance Center script and call it on error using the system (command); function described in the initial blog post. This would kill for example a rogue Citrix or SAPGUI client and leave others running during a performance test.

 


[CmdletBinding()]     

( [string[]]$ComputerName = $env:ComputerName)           

($Computer in $ComputerName)

if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet))

Write-Host "$Computer : OFFLINE"           

Continue           

try {

$Processes = Get-Process -ComputerName $Computer -EA Stop

$nProcesses = @($Processes | ? { $_.Responding -eq $false })

catch {

Write-Error "Failed to query processes. $_"

if($nProcesses)

foreach($nProcess in $nProcesses)

$nProcess | select Name, id, MainWindowTitle, Responding

Write-host "Attempting to kill stalled process"

if ($nProcess.Id -ne $PID)

Stop-Process $nProcess.Id}

else

Write-host "No non-responding processes found"

Author: Richard Bishop

http://about.me/richard_bishop

Leave a Reply

Your email address will not be published. Required fields are marked *