How to get IIS AppPool Worker Process ID

23

I have a PowerShell script that is run automatically when our monitoring service detects that a website is down. It is supposed to stop the AppPool (using Stop-WebAppPool -name $AppPool;), wait until it is really stopped and then restart it.

Sometimes it the process does not actually stop, manifested by the error

Cannot Start Application Pool:  
The service cannot accept control messages at this time.
(Exception from HRESULT: 0x80070425)"

when you try to start it again.

If it takes longer than a certain number of seconds to stop (I will chose that amount of time after I have timed several stops to see how long it usually takes), I want to just kill the process.

I know that I can get the list of processes used by workers in the AppPool by doing dir IIS:\AppPools\MyAppPool\WorkerProcesses\,

Process ID  State      Handles  Start Time
----------  -----      -------  ----------
7124        Running

but I can't figure out how to actually capture the process id so I can kill it.

iis
powershell
automation
asked on Stack Overflow Aug 10, 2011 by yakatz

3 Answers

34

In case that Process ID is really the id of process to kill, you can:

$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
Stop-Process -id $id

or

dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | % { Stop-Process -id $_.processId }
answered on Stack Overflow Aug 10, 2011 by stej
14

In Command Prompt on the server, I just do the following for a list of running AppPool PIDs so I can kill them with taskkill or Task Mgr:

cd c:\windows\system32\inetsrv
appcmd list wp

taskkill /f /pid *PIDhere*
answered on Stack Overflow Apr 18, 2013 by PandaCookie
10

(Adding answer from Roman's comment, since there maybe cache issues with stej's solution)

Open Powershell as an Administrator on the web server, then run:

gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId 

You should see something like:

AppPoolName ProcessId
----------- ---------
AppPool_1        8020
AppPool_2        8568

You can then use Task Manager to kill it or in Powershell use:

Stop-Process -Id xxxx

If you get Get-WmiObject : Could not get objects from namespace root/WebAdministration. Invalid namespace then you need to enable the IIS Management Scripts and Tools feature using:

ipmo ServerManager
Add-WindowsFeature Web-Scripting-Tools
answered on Stack Overflow Dec 10, 2013 by Greg Bray • edited Dec 17, 2013 by Greg Bray

User contributions licensed under CC BY-SA 3.0