【发布时间】:2016-03-08 16:10:54
【问题描述】:
我目前正在尝试查找在客户端计算机上运行的命令行,如果找到运行脚本的命令行,我需要终止该进程 ID。这是我目前拥有的,但我对杀死该 ParentProcessID 的好方法有点迷茫。
您可以在我的 Get-WMIObject 中看到,我正在获取 CommandLine 和 ParentProcess ID 的属性。我可以运行 foreach 并将这些命令行与字符串匹配。但此时,我不知道如何传递或链接 ParentProcessID 属性,以便我可以终止该 ParentProcessID。
$process = "powershell.exe"
$GetCommand = Get-WmiObject Win32_Process -Filter "name = '$process'" |select CommandLine, ParentProcessID
foreach($command in $GetCommand){
If($command -match "MyScript.ps1"){
#kill ParentProcessID
}
}
任何想法我将如何做到这一点?
【问题讨论】:
-
if($command.CommandLine -match "MyScript.ps1"){ Stop-Process -Id $command.ParentProcessID } -
谢谢马蒂亚斯!我不认为我可以通过这种方式通过主变量传递属性。哇,我学到了新东西。谢谢你。我不知道如何将其标记为已回答,以便您获得荣誉。
-
这是 PowerShell - 一切都是 .NET 对象 :)
标签: powershell variables get-wmiobject