Powershell 如何将Get wmioobject属性传递到变量中
我目前正试图找到一个在客户端计算机上运行的命令行,如果找到了运行脚本的命令行,我需要终止该进程id。这是我目前拥有的,但我有点不知道用什么好的方法杀死该ParentProcessIDPowershell 如何将Get wmioobject属性传递到变量中,powershell,variables,get-wmiobject,Powershell,Variables,Get Wmiobject,我目前正试图找到一个在客户端计算机上运行的命令行,如果找到了运行脚本的命令行,我需要终止该进程id。这是我目前拥有的,但我有点不知道用什么好的方法杀死该ParentProcessID $process = "powershell.exe" $GetCommand = Get-WmiObject Win32_Process -Filter "name = '$process'" |select CommandLine, ParentProcessID foreach($command in $G
$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
}
}
您可以在我的Get-WMIOObject中看到,我获得了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
}
}
你知道我将如何实现这一点吗?在PowerShell中(与传统的shell不同)——所有东西都是一个包装好的.NET对象
这意味着您可以使用
操作符引用使用选择对象选择的属性
$process = "powershell.exe"
$GetCommand = Get-WmiObject Win32_Process -Filter "name = '$process'" |Select-Object CommandLine, ParentProcessID
foreach($command in $GetCommand){
if($command.CommandLine -match "MyScript.ps1"){
Stop-Process -Id $command.ParentProcessID
}
}
if($command.CommandLine-match“MyScript.ps1”){Stop Process-Id$command.ParentProcessID}
谢谢Mathias!我不认为我可以通过这种方式通过主变量传递属性。哇,我学到了一些新东西。非常感谢。我不知道如何将此标记为已回答,以便您获得信任。这是PowerShell-所有内容都是.NET对象:)