【问题标题】:Kill process by ParentProcessID通过 ParentProcessID 杀死进程
【发布时间】:2015-11-24 10:31:39
【问题描述】:

我想通过 ParentProcessID 杀死一个正在运行的进程。我想像你在命令行中那样做:

wmic process where parentprocessid= 3008 terminate 

但现在问题是,在 PowerShell 中,我将 ParentProcessID 作为一个变量,如下所示:

$p = 3008

现在我想通过变量 $p 终止进程,但这不起作用:

wmic process where parentprocessid= $p terminate

如果我将 ParentProcessID 存储在变量中,我如何通过其 ParentProcessID 终止进程?

【问题讨论】:

  • 嗨,你为什么不使用Get-Process?你绝对应该使用 wmic?
  • wmic process where parentprocessid=$p terminate
  • @kekimian - Get-Process 不会为您提供有关父进程的信息
  • @Matt 标记编辑将允许您在同一编辑中仅更改标题/正文中的一个字符 :)
  • @MathiasR.Jessen 我知道。当有其他事情要做时,我不喜欢进行小片段编辑。也不在乎和OP打架就可以了。

标签: powershell wmi wmic


【解决方案1】:

使用Get-WmiObject 检索Win32_Process 对象并将其通过管道传递给Invoke-WmiMethod 以调用Terminate 方法:

Get-WmiObject Win32_Process -Filter "ParentProcessId=$p" | Invoke-WmiMethod Terminate

【讨论】:

    【解决方案2】:

    试试这个:

    $parentId = 3008
    $name = "Process name"
    
    Get-WmiObject -Class Win32_Process | 
    where {$_.ParentProcessId -eq $parentId -and $_.Name -eq $name} | 
    foreach {$_.terminate(0)}
    

    添加了$name 参数,因为可能有多个子进程。如果您需要杀死他们,请跳过-and $_.Name -eq $name

    【讨论】:

      【解决方案3】:

      我找到了解决办法,就是去掉“=”和变量名之间的空格。

      wmic process where parentprocessid=$p terminate
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 2020-03-02
        • 2014-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-23
        相关资源
        最近更新 更多