[英]Using Powershell to obtain an IIS Application Pool ProcessID
I have searched quite a bit for options to get the app Pool process id for a powershell script.我已经搜索了很多选项来获取 powershell 脚本的应用程序池进程 ID。 The issue I am having is most solutions I find point to using the WebAdministration WorkerProcess.
我遇到的问题是我发现大多数解决方案都指向使用 WebAdministration WorkerProcess。 I tried the below script on Windows Server 2012 (IIS 8) and Windows Server 2019 (IIS 10).
我在 Windows Server 2012 (IIS 8) 和 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.运行
Get-WmiObject -NameSpace 'root\\WebAdministration' -List
提供相当大的项目列表,但 WorkerProcesses 不是其中之一。
I've also tried dir IIS:\\AppPools\\$AppPool\\WorkerProcesses\\
which also provides no results.我也试过
dir IIS:\\AppPools\\$AppPool\\WorkerProcesses\\
也没有提供任何结果。
How can I get the processId of a specific application pool?如何获取特定应用程序池的 processId? or if that is no longer possible, how would I be able to get the cpu and memory consumption of specific application pools?
或者如果这不再可能,我将如何获得特定应用程序池的 CPU 和内存消耗?
Try piping to get some extra info:尝试通过管道获取一些额外信息:
$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
$id
Thanks @dai for pointing out that the process may not even be running due to lack of web requests.感谢@dai 指出由于缺乏网络请求,该进程甚至可能无法运行。 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.
所以就我而言 - 没有结果是一件好事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.