2

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

Tim Ververs
  • 527
  • 9
  • 23
  • Why would you want a process dump *after* the recycle? It's a new, freshly spun up process, nothing interesting to find in there – Mathias R. Jessen Jul 20 '15 at 13:03
  • I'm creating a watcher that goes off when the process hits >90% of the CPU. Apperently the first call after a recycles makes the w3wp.exe process go to 100% cpu. I want a process dump of that – Tim Ververs Jul 20 '15 at 13:08
  • I'm pretty sure that's by design, see ["Compiling on first request" in this article](https://msdn.microsoft.com/en-us/library/ms366723(v=vs.140).aspx) – Mathias R. Jessen Jul 20 '15 at 13:30
  • 2
    You can use /text to return only the id: `$WebWorker = appcmd list wp /apppool.name:"MyApp_Web" /text:WP.NAME` – Fábio Uechi Oct 09 '19 at 20:32

1 Answers1

1

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.

Vesper
  • 18,599
  • 6
  • 39
  • 61