1

I have searched quite a bit for options to get the app Pool process id for a powershell script. The issue I am having is most solutions I find point to using the WebAdministration WorkerProcess. I tried the below script on Windows Server 2012 (IIS 8) and Windows Server 2019 (IIS 10).

Get-ChildItem -Path IIS:\AppPools |%{
    $AppPool = $_.Name
    Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' | Where-Object {$_.AppPoolName -match $AppPool} | Select-Object -Expand ProcessId | ForEach-Object {
            $AppPoolPID=$_
            $AppPoolProcces = Get-Wmiobject -Class Win32_PerfFormattedData_PerfProc_Process | Where-Object { $_.IdProcess -eq $AppPoolPID } 
            $AppPoolCpu = $AppPoolProcces.PercentProcessorTime
            $AppPoolMemory = [Math]::Round(($AppPoolProcces.workingSetPrivate / 1MB),2)
            $Cpu += $AppPoolCpu
            $Memory += $AppPoolMemory
            Write-Host "Application pool $AppPool process id: $_ Percent CPU: $Cpu Private Memory: $Memory"
    }
}

Running Get-WmiObject -NameSpace 'root\WebAdministration' -List Provides a rather large list of items, but WorkerProcesses is not one of them.

I've also tried dir IIS:\AppPools\$AppPool\WorkerProcesses\ which also provides no results.

How can I get the processId of a specific application pool? or if that is no longer possible, how would I be able to get the cpu and memory consumption of specific application pools?

kg123
  • 11
  • 6
  • How is the Application Pool configured? If it's in "Web Garden" mode then you won't see a _single_ Process-Id. Also, does the Application Pool process-id appear correctly inside IIS Manager? – Dai Jun 24 '21 at 23:21
  • it is not in Web Garden Mode (Max processes allowed = 1). No worker Processes show up in IIS either. I am assuming if I can get them to show up there, then the script will work. – kg123 Jun 25 '21 at 00:34
  • Is the application even running? Note that IIS won’t pre-start applications by default. You need to make a HTTP request to them first. – Dai Jun 25 '21 at 01:56
  • You won't get anything as easy as `appcmd list wp`. – Lex Li Jun 25 '21 at 03:45
  • @LexLi appcmd does not appear to exist, could you provide the full path? – kg123 Jun 25 '21 at 14:10
  • @Dai I think that's it. I am running this on a test server that has very little traffic. Thanks! I will post this as the answer I was looking for. – kg123 Jun 25 '21 at 14:13
  • `PS C:\Windows\system32\inetsrv> .\appcmd.exe list wp` – Lex Li Jun 25 '21 at 20:38
  • @LexLi thanks. I will keep that in mind for the future. – kg123 Jul 02 '21 at 19:47

3 Answers3

1

Try piping to get some extra info:

$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
$id
Technoob1984
  • 172
  • 9
  • Thanks for the response. I had run accross this in some other posts, but as I mentioned in the question, the base dir command did not provide any results so nothing was being piped into the select-object command. I should note, your command does work when workerprocesses are present. @dai I believe provided the actual answer with the question "is the application even running?" – kg123 Jun 25 '21 at 13:50
  • @kg123, how would there be a process ID if there wasn't a process? I think the command I gave will help you. Two other things you can do to help: 1. Give each app pool it's own ID, only use one App Pool per Web Application. 2. SPNs might help too depending on the exact information you need. They are more secure as well, but do have a little administrative overhead: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/register-a-service-principal-name-for-kerberos-connections?view=sql-server-ver15 – Technoob1984 Jun 28 '21 at 15:15
  • btw, when I run this I get `Get-ChildItem: Cannot find drive. A drive with the name 'IIS' does not exist.` – KyleMit Jun 28 '23 at 17:57
  • 1
    Hey @KyleMit, That is a common call. Here is a link to some other examples. I am not sure why it isn't working, but maybe try posting a thread and we can dig into it. Thanks https://stackoverflow.com/questions/7014536/how-to-get-iis-apppool-worker-process-id – Technoob1984 Jul 15 '23 at 01:34
0

Thanks @dai for pointing out that the process may not even be running due to lack of web requests. I was under the impression that it would always be running.

The script portion as I posted initially does actually work. So for my purposes - no results is a good thing.

kg123
  • 11
  • 6
0

You can use appcmd.exe to list all running worker processes like this:

C:\Windows\System32\inetsrv\appcmd list wp

Output

WP "16474" (applicationPool:testSite1)
WP "30531" (applicationPool:testSite2)

If you want to return just the process ID for just one app pool, you can call like this:

C:\Windows\System32\inetsrv\appcmd list wp /apppool.name:"testSite1" /text:WP.NAME

Output

16474

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664