如何获取 IIS AppPool 工作进程 ID

发布于 2024-11-29 07:16:26 字数 660 浏览 0 评论 0原文

我有一个 PowerShell 脚本,当我们的监控服务检测到网站关闭时,该脚本会自动运行。 它应该停止AppPool(使用Stop-WebAppPool -name $AppPool;),等到它真正停止然后重新启动它。

有时,该进程实际上并未停止,

Cannot Start Application Pool:  
The service cannot accept control messages at this time.
(Exception from HRESULT: 0x80070425)"

当您尝试再次启动它时会出现错误。

如果停止时间超过一定秒数(我会在多次停止计时后选择该时间量,看看通常需要多长时间),我想终止该进程。

我知道我可以通过执行 dir IIS:\AppPools\MyAppPool\WorkerProcesses\ 来获取 AppPool 中工作人员使用的进程列表,

Process ID  State      Handles  Start Time
----------  -----      -------  ----------
7124        Running

但我不知道如何实际捕获进程 id这样我就可以杀死它。

I have a PowerShell script that is run automatically when our monitoring service detects that a website is down.
It is supposed to stop the AppPool (using Stop-WebAppPool -name $AppPool;), wait until it is really stopped and then restart it.

Sometimes it the process does not actually stop, manifested by the error

Cannot Start Application Pool:  
The service cannot accept control messages at this time.
(Exception from HRESULT: 0x80070425)"

when you try to start it again.

If it takes longer than a certain number of seconds to stop (I will chose that amount of time after I have timed several stops to see how long it usually takes), I want to just kill the process.

I know that I can get the list of processes used by workers in the AppPool by doing dir IIS:\AppPools\MyAppPool\WorkerProcesses\,

Process ID  State      Handles  Start Time
----------  -----      -------  ----------
7124        Running

but I can't figure out how to actually capture the process id so I can kill it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漆黑的白昼 2024-12-06 07:16:26

如果进程 ID 确实是要杀死的进程的 ID,您可以:

$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
Stop-Process -id $id

dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | % { Stop-Process -id $_.processId }

In case that Process ID is really the id of process to kill, you can:

$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
Stop-Process -id $id

or

dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | % { Stop-Process -id $_.processId }
椒妓 2024-12-06 07:16:26

在服务器上的命令提示符中,我只需对正在运行的 AppPool PID 列表执行以下操作,以便我可以使用 taskkill 或 Task Mgr 杀死它们:

cd c:\windows\system32\inetsrv
appcmd list wp

taskkill /f /pid *PIDhere*

In Command Prompt on the server, I just do the following for a list of running AppPool PIDs so I can kill them with taskkill or Task Mgr:

cd c:\windows\system32\inetsrv
appcmd list wp

taskkill /f /pid *PIDhere*
作业与我同在 2024-12-06 07:16:26

(添加 Roman 评论中的答案,因为 stej 的解决方案可能存在缓存问题)

在 Web 服务器上以管理员身份打开 Powershell,然后运行:

gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId 

您应该看到类似以下内容:

AppPoolName ProcessId
----------- ---------
AppPool_1        8020
AppPool_2        8568

然后您可以使用任务管理器来终止它或在 Powershell 中使用:

Stop-Process -Id xxxx

如果您得到 Get-WmiObject : 无法从命名空间 root/WebAdministration 获取对象。命名空间无效,那么您需要使用以下命令启用IIS 管理脚本和工具功能:

ipmo ServerManager
Add-WindowsFeature Web-Scripting-Tools

(Adding answer from Roman's comment, since there maybe cache issues with stej's solution)

Open Powershell as an Administrator on the web server, then run:

gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId 

You should see something like:

AppPoolName ProcessId
----------- ---------
AppPool_1        8020
AppPool_2        8568

You can then use Task Manager to kill it or in Powershell use:

Stop-Process -Id xxxx

If you get Get-WmiObject : Could not get objects from namespace root/WebAdministration. Invalid namespace then you need to enable the IIS Management Scripts and Tools feature using:

ipmo ServerManager
Add-WindowsFeature Web-Scripting-Tools
别闹i 2024-12-06 07:16:26

我也遇到了同样的问题,这是我的解决方案:

Get-WmiObject –class win32_process -filter 'name="w3wp.exe"' | Select-Object –Property Name, ProcessId, @{n='AppPool';e={$_.GetOwner().user}} | Where-Object {$_.AppPool -eq "WebsiteName"} | Select-Object -expand ProcessId

I also had the same problem, and here is my solution:

Get-WmiObject –class win32_process -filter 'name="w3wp.exe"' | Select-Object –Property Name, ProcessId, @{n='AppPool';e={$_.GetOwner().user}} | Where-Object {$_.AppPool -eq "WebsiteName"} | Select-Object -expand ProcessId

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文