如何将 Get-WMIObject 属性传递给变量
How can I pass Get-WMIObject Properties into a variable
我目前正在尝试在客户端计算机上找到 运行ning 的命令行,如果找到命令行 运行ning 脚本,我需要终止该进程 ID .这是我目前拥有的,但我有点迷失了杀死 ParentProcessID 的好方法。
您可以在我的 Get-WMIObject 中看到,我正在获取 CommandLine 和 ParentProcess ID 的属性。我可以 运行 foreach 并将这些命令行与字符串匹配。但在这一点上,我不知道如何传递或 link 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 对象。
这意味着您可以使用 .
运算符
引用通过 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
}
}
我目前正在尝试在客户端计算机上找到 运行ning 的命令行,如果找到命令行 运行ning 脚本,我需要终止该进程 ID .这是我目前拥有的,但我有点迷失了杀死 ParentProcessID 的好方法。
您可以在我的 Get-WMIObject 中看到,我正在获取 CommandLine 和 ParentProcess ID 的属性。我可以 运行 foreach 并将这些命令行与字符串匹配。但在这一点上,我不知道如何传递或 link 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 对象。
这意味着您可以使用 .
运算符
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
}
}