How to get process id by its service name with a script to variable

前端 未结 6 1323
闹比i
闹比i 2021-01-01 13:37

I have service named WinDefend and it runs on process svchost.exe
There other many svchost.exe processes and I need to find a way

6条回答
  •  时光说笑
    2021-01-01 14:20

    tasklist is just returning text, not actual objects that have properties you can access. You can use WMI to get this information instead:

    $id = Get-WmiObject -Class Win32_Service -Filter "Name LIKE 'WinDefend'" | 
          Select-Object -ExpandProperty ProcessId
    
    $process = Get-Process -Id $id
    

提交回复
热议问题