Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Answer

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 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...