Powershell 按ParentProcessID终止进程

Powershell 按ParentProcessID终止进程,powershell,wmi,wmic,Powershell,Wmi,Wmic,我想通过ParentProcessID终止正在运行的进程。我想像你在命令行中那样做: wmic process where parentprocessid= 3008 terminate 但现在的问题是,在PowerShell中,我将ParentProcessID作为一个变量,如下所示: $p = 3008 现在我想用varibale$p终止进程,但这不起作用: wmic process where parentprocessid= $p terminate 如果ParentProces

我想通过ParentProcessID终止正在运行的进程。我想像你在命令行中那样做:

wmic process where parentprocessid= 3008 terminate 
但现在的问题是,在PowerShell中,我将ParentProcessID作为一个变量,如下所示:

$p = 3008
现在我想用varibale
$p
终止进程,但这不起作用:

wmic process where parentprocessid= $p terminate

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

使用
Get WmiObject
检索Win32\u进程对象,并将其传送到
Invoke WmiMethod
以调用
Terminate
方法:

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

使用
Get WmiObject
检索Win32_进程对象,并将其传送到
Invoke WmiMethod
以调用
Terminate
方法:

Get-WmiObject Win32_Process -Filter "ParentProcessId=$p" | Invoke-WmiMethod Terminate
试试这个:

$parentId = 3008
$name = "Process name"

Get-WmiObject -Class Win32_Process | 
where {$_.ParentProcessId -eq $parentId -and $_.Name -eq $name} | 
foreach {$_.terminate(0)}
添加了
$name
参数,因为可能有多个子进程。如果你需要杀死他们,只需跳过
-和$\uu.Name-eq$Name

试试以下方法:

$parentId = 3008
$name = "Process name"

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

添加了
$name
参数,因为可能有多个子进程。如果你需要杀死他们,只需跳过
-和$\u.Name-eq$Name

我找到了解决方案,只需删除“=”和变量名之间的空格

wmic process where parentprocessid=$p terminate

我找到了解决方案,只是删除了“=”和变量名之间的空格

wmic process where parentprocessid=$p terminate

您好,为什么不使用
获取过程
?您绝对应该使用wmic?
wmic进程,其中parentprocessid=$p terminate
@kekimian-
Get进程
不提供有关父进程的信息process@Matt标记编辑将允许您在同一编辑中仅更改标题/正文中的一个字符:)@MathiasR.Jessen I know。我不喜欢在有其他事情要做的时候做一些小的剪贴编辑。我也不想和OP打交道。嗨,你为什么不使用
Get进程
?您绝对应该使用wmic?
wmic进程,其中parentprocessid=$p terminate
@kekimian-
Get进程
不提供有关父进程的信息process@Matt标记编辑将允许您在同一编辑中仅更改标题/正文中的一个字符:)@MathiasR.Jessen I know。我不喜欢在有其他事情要做的时候做一些小的剪贴编辑。我也不想和OP吵架。