简体   繁体   中英

How to get IIS AppPool Worker Process ID

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.

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 }

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*

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM