简体   繁体   English

在给定进程 ID 的情况下终止 PowerShell 中的进程树

[英]Terminate process tree in PowerShell given a process ID

Lets say I run a couple of processes from PowerShell:假设我从 PowerShell 运行几个进程:

$p1 = $(Start-Process -PassThru ./example.exe)
$p2 = $(Start-Process -PassThru ./example.exe)

example.exe is going to spawn a few child processes with the same name. example.exe将生成几个具有相同名称的子进程。

How do I kill just $p1 and its child processes, without killing $p2 and its child processes?如何杀死$p1及其子进程,而不杀死$p2及其子进程?

Just running Stop-Process $p1 only kills the parent process $p1 , leaving it's children running.仅运行Stop-Process $p1只会杀死父进程$p1 ,使其子进程运行。

All of the answers I seen so far involve killing all of the processes with a certain name, but that will not work here.到目前为止,我看到的所有答案都涉及杀死具有特定名称的所有进程,但这在这里不起作用。

So I couldn't really find a good way to do this, so I wrote a helper function that uses recursion to walk down the process tree:所以我真的找不到一个好的方法来做到这一点,所以我写了一个帮助函数,它使用递归来遍历进程树:

function Kill-Tree {
    Param([int]$ppid)
    Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $ppid } | ForEach-Object { Kill-Tree $_.ProcessId }
    Stop-Process -Id $ppid
}

To use it, put it somewhere in your PowerShell script, and then just call it like so:要使用它,请将其放在 PowerShell 脚本中的某个位置,然后像这样调用它:

Kill-Tree <process_id>

Start-Process with -Passthru returns a System.Diagnostics.Process object. Start-Process with -Passthru返回一个System.Diagnostics.Process对象。 This object has an Id property which is generated by Windows and is unique.此对象具有由 Windows 生成的唯一的Id 属性

Stop-Process has multiple signatures One looks for the process by Id, one by Name, and one looks for it by the Process object. Stop-Process多个签名,一个按Id 查找进程,一个按Name 查找,一个按Process 对象查找。

You are using -Passthru and capturing the output of start-process into $p1 , so you can just pass that object to Stop-Process.您正在使用-Passthru并将start-process的输出捕获到$p1中,因此您可以将该对象传递给 Stop-Process。

Stop-Process $p1

If anyone landing here is looking for script to kill a process tree by specifying the name of the parent process, you can use @wheeler's Kill-Tree recursive function like so.如果登陆这里的任何人正在寻找脚本来通过指定父进程的名称来终止进程树,您可以像这样使用@wheeler 的 Kill-Tree 递归函数。

Note: By default, I find myself having to constantly kill eclipse and its child java process.注意:默认情况下,我发现自己不得不不断地杀死 eclipse 及其子 java 进程。 So 'eclipse' is the default name of the process tree killed by this script, killProcTree.ps1 , if a parent process name is not provided via the $procName call parameter:因此,如果未通过 $ procName调用参数提供父进程名称,则 'eclipse' 是该脚本杀死的进程树的默认名称killProcTree.ps1 :

Powershell script called 'killProcTree.ps1':名为“killProcTree.ps1”的 Powershell 脚本:


param(
    [string]$procName    = 'eclipse'
)

function Kill-Tree {
    Param([int]$ppid)
    echo "Kill-Tree: killing $ppid ..."
    Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $ppid } | ForEach-Object { Kill-Tree $_.ProcessId }
    Stop-Process -Id $ppid
}

[int]$mypid = 0
[string]$myProcessToKill = (Get-Process -Name $procName -ErrorAction 0)

if ($myProcessToKill -eq "") {
    echo "$procName is not running."
} else {
    $mypid = (Get-Process -Name $procName -ErrorAction 0).Id
    echo "The $procName PID is: $mypid"
    if ($mypid -gt 1) {
        echo "Killing $procName and its children..."
        Kill-Tree $mypid
    }
}
echo "Done!"

我知道这个答案来晚了......但是实现这一点的更简单的方法(与 PowerShell 5.1+ 一起使用)是:

Get-Process -Name "Process Name" | Select-Object -Property Id | ForEach-Object -Process { Stop-Process -Id $_.Id -Force }

Hi I'm noob and I don't know if that's what you mean but I figured out that if I want to kill a process that is visible in task manager I can do it like that: (name of the process provided in -> "name of the process" below. I pref live examples so I will present it that way)嗨,我是菜鸟,我不知道这是否是您的意思,但我发现如果我想杀死在任务管理器中可见的进程,我可以这样做:(在 -> 中提供的进程名称下面的“过程名称”。我喜欢现场示例,所以我会以这种方式呈现)

Get-Process -Name "Battle.net" | kill

It kills all process ID's related to that process which contain the name "Battle.net"它会杀死与该进程相关的所有进程 ID,其中包含名称“Battle.net”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM