2016-03-08 25 views
0

我正在嘗試查找在客戶端計算機上運行的命令行,並且如果找到運行腳本的命令行,我需要終止該進程ID。這是我目前所擁有的,但是我有點失去了一個殺死ParentProcessID的好方法。如何將Get-WMIObject屬性傳遞給變量

你可以在我的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 
    } 

} 

任何想法,我會做到這一點?

+0

'如果($ command.CommandLine -match 「MyScript.ps1」){停止進程-Id $ command.ParentProcessID引用所選擇的屬性與Select-Object }' –

+0

謝謝Mathias!我不認爲我可以這樣通過主變量傳遞屬性。哇,我學到了一些新東西。 謝謝。我不確定如何將其標記爲已回答,因此您可以獲得相應的回報。 – TheInfamousOne

+0

這是PowerShell - 一切都是.NET對象:) –

回答

0

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