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?