Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何将Get wmioobject属性传递到变量中_Powershell_Variables_Get Wmiobject - Fatal编程技术网

Powershell 如何将Get wmioobject属性传递到变量中

Powershell 如何将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

我目前正试图找到一个在客户端计算机上运行的命令行,如果找到了运行脚本的命令行,我需要终止该进程id。这是我目前拥有的,但我有点不知道用什么好的方法杀死该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
    }

 }
您可以在我的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对象:)