我需要一个允许我终止进程树进程的命令。
例如notepad.exe
是由资源管理器创建的。我如何终止notepad.exe
进程树中的explorer.exe
?
使用taskkill /IM <processname.exe> /T
。
尝试使用PsTools set中的PsKill + PsList实用程序。
pslist -t
会给你一个进程树(在这里你可以找到notepad.exe
这是explorer.exe
的子进程。然后你可以使用pskill
来杀死具有指定id的进程。
taskkill /F /IM notepad.exe
这将杀死所有notepad.exe
- 如果你想要一种方法来指定只杀死由notepad.exe
创建的foo.exe
,我不认为Windows命令行对此有足够的强大。
您可以使用tasklist
获取要定位的进程的进程ID,然后使用taskkill /F /PID <PID>
来终止它。
现在,您可以使用PowerShell执行此操作:
$cimProcesses = Get-CimInstance -Query "select ProcessId, ParentProcessId from Win32_Process where Name = 'notepad.exe'"
$processes = $cimProcesses | Where-Object { (Get-Process -Id $_.ParentProcessId).ProcessName -eq "explorer" } | ForEach-Object { Get-Process -Id $_.ProcessId }
$processes.Kill()
或者优雅的方式:
$cimProcesses = Get-CimInstance -Query "select ProcessId, ParentProcessId from Win32_Process where Name = 'notepad.exe'"
$cimProcesses = $cimProcesses | Where-Object { (Get-Process -Id $_.ParentProcessId).ProcessName -eq "explorer" }
$cimProcesses | ForEach-Object { taskkill /pid $_.ProcessId }