是否可以通过PowerShell获取正在运行的服务的PID?
$srv = Get-Service MyTestService
$myPid = ... ?
Get-CimInstance
cmdlet 与 Win32_Service
WMI 类结合使用:
$svc = Get-Service MyTestService
$svcPid =
(Get-CimInstance Win32_Service -Filter "Name = `"$($svc.Name)`"").ProcessId
请注意,如果目标服务未运行,结果将为
0
。