【问题标题】:How can I pass Get-WMIObject Properties into a variable如何将 Get-WMIObject 属性传递给变量
【发布时间】: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


【解决方案1】:

在 PowerShell 中(与传统 shell 不同)- 一切都是一个包装好的 .NET 对象。

这意味着您可以使用. 运算符引用Select-Object 选择的属性

$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
    }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2019-06-05
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多