Search code examples
powershelliisrecyclepid

Get process ID based on apppool after a recycle


Our apppools recycle at around 02:00 nightly. I want to create a processdump after the recycle but the process ID changes after a recycle.

How can i get the process ID of the recycled apppool based on the apppool identity (Username running the process) or apppool name?

I tried running:

appcmd list wp

This gave me the names + id's. How can i filter this result to save only the Id based on a specific name into a variable using powershell?

I was thinking about something like :

$processId = appcmd list wp | Where-Object {$_.apppool -eq "MyApp_web"}

Thanks in advance,

Tim

EDIT:

$workerprocess = appcmd list wp /apppool.name:"MyApp_Web"

returns the following: WP "6816" (applicationPool:MyApp_Web)

Now i just want the ID of this proces, not all the other data

EDIT 2 : Thanks to Vesper

$WebWorker = appcmd list wp /apppool.name:"MyApp_Web"

if ($WebWorker -match "\d+") { $WebId=$matches[0] }

Write-Host


Solution

  • In your case, all you need to do is get the PID out of the string. Since a PID is a sequence of digits, you can use a regex-match to get the matching region, like this:

    if ($workerprocess -match "\d+") { $pid=$matches[0] }
    

    This will return 6816 out of your string.