Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

powershell - 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:AppPoolsMyAppPoolWorkerProcesses,

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

$id = dir IIS:AppPoolsMyAppPoolWorkerProcesses | Select-Object -expand processId
Stop-Process -id $id

or

dir IIS:AppPoolsMyAppPoolWorkerProcesses | % { Stop-Process -id $_.processId }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...