通过 ParentProcessID 终止进程
Kill process by ParentProcessID
我想通过 ParentProcessID 终止 运行 进程。我想像您在命令行中那样做:
wmic process where parentprocessid= 3008 terminate
但现在的问题是,在 PowerShell 中,我将 ParentProcessID 作为变量,如下所示:
$p = 3008
现在我想通过变量终止进程 $p
但这不起作用:
wmic process where parentprocessid= $p terminate
如果我将 ParentProcessID 存储在变量中,我如何通过其 ParentProcessID 终止进程?
使用 Get-WmiObject
检索 Win32_Process 对象并将其通过管道传递给 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
参数,因为可能有多个子进程。如果你需要杀光他们就跳过 -and $_.Name -eq $name
我找到了解决办法,就是去掉“=”和变量名之间的space。
wmic process where parentprocessid=$p terminate
我想通过 ParentProcessID 终止 运行 进程。我想像您在命令行中那样做:
wmic process where parentprocessid= 3008 terminate
但现在的问题是,在 PowerShell 中,我将 ParentProcessID 作为变量,如下所示:
$p = 3008
现在我想通过变量终止进程 $p
但这不起作用:
wmic process where parentprocessid= $p terminate
如果我将 ParentProcessID 存储在变量中,我如何通过其 ParentProcessID 终止进程?
使用 Get-WmiObject
检索 Win32_Process 对象并将其通过管道传递给 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
参数,因为可能有多个子进程。如果你需要杀光他们就跳过 -and $_.Name -eq $name
我找到了解决办法,就是去掉“=”和变量名之间的space。
wmic process where parentprocessid=$p terminate