Powershell to aggressively force an IIS web worker process (w3wp) to stop. Useful when you need to manually de-pool a web node in a hurry, but you have a long running process in the app pool that prevents the process from stopping using "stop-service".
The AppPoolName parameter allows you to select the specific w3wp.exe based on the app pool name (they're difficult to tell apart in script). The script could be extended to return a status when the app pool name isn't found.
Usage is Kill-AppPool -AppPoolName name_of_application
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)] [string] $AppPoolName
)
function Kill-AppPool([string]$name="*"){
$list = get-process w3wp
foreach($p in $list){
$filter = "Handle='" + $p.Id + "'"
$wmip = get-WmiObject Win32_Process -filter $filter
if($wmip.CommandLine -match "-ap `"([a-zA-Z0-9`.]+)`""){
$appName = $matches[1]
$p | add-member NoteProperty AppPoolName $appName
}
}
$filtererList = ($list | where { $_.AppPoolName -like $name })
if($filtererList.Count -gt 1){ throw "'$name' matched more than one appPools. You must enter a unique name." }
taskkill /F /PID $filtererList.Id
return $filtererList.Id
}
Kill-AppPool $AppPoolName
No comments:
Post a Comment