如何从相同的 运行 进程获取 pid 并将输出通过管道传输到 if 语句

How to get pid from identical running processes and pipe the output into if statement

我希望脚本找到两个当前正在 运行ning 的相同进程,并通过使用 If($_.Path -eq $procName){命令。

根据之前找到的完整文件路径识别出正确的进程后,我想使用它的唯一 PID 来检查它当前是否 suspended or not suspended

如果当前管道一切顺利,则将其输出值用于运行以下两个命令之一:

If($_.Path -eq $procName) {
    Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner $myPID"
} Else {
    Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner -r $myPID"
}

我的问题是无论进程当前是否挂起,输出都是相同的结果 Start-Process cmd.exe -WindowStyle Maximized -ArgumentList /D /K pssuspend.exe -nobanner -r 18960

这是我目前拥有的:

$procName = 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'
$procID = Get-Process -Name 'RocketLeague' | Select-Object -Property Id,Path | ForEach-Object {
    If($_.Path -eq $procName){
        $myPID = $_.Id
        If(Get-ChildItem | Where-Object -FilterScript {$_.Responding -eq $false}) {
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner $myPID"
            } Else {
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner -r $myPID"
        }
    }
}

根据代码并尊重 Santiago 已经指出的内容,您似乎正在尝试根据特定进程是否响应来采取某些操作。如果我有那个权利,我认为 Get-ChildItem 没有必要。您可以附加 Select-Object 命令以包含 Responding 属性 然后执行如下逻辑:

$procName = 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'

$procID = 
Get-Process -Name 'RocketLeague' | 
Select-Object -Property Id, Path, Responding |
ForEach-Object {
    If($_.Path -eq $procName){
        $myPID = $_.Id
        If( !$_.Responding ) {        
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner $myPID"
            } 
        Else {
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner -r $myPID"
        }
    }
}

但是,除非脚本有更多内容,否则我认为您根本不需要 Select-Object 命令:

$procName = 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'

$procID = 
Get-Process -Name 'RocketLeague' | 
ForEach-Object {
    If($_.Path -eq $procName){
        $myPID = $_.Id
        If( !$_.Responding ) {        
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner $myPID"
            } 
        Else {
            Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner -r $myPID"
        }
    }
}

此外,$procID 变量中不会有任何内容,因为后续代码不会向管道写入任何内容。我留下它只是因为我不知道完整的意图。

您还可以通过将一些 If 逻辑移动到 Where-Object 子句中来稍微缩短代码,请参见下面删除的 $ProcID

$procName = 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'

Get-Process -Name 'RocketLeague' | 
Where-Object{ $_.Path -eq $procName }|
ForEach-Object {    
    If( !$_.Responding ) {        
        Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner $($_.Id)"
        } 
    Else {
        Write-Host Start-Process cmd.exe -WindowStyle Maximized -ArgumentList "/D /K pssuspend.exe -nobanner -r $($_.Id)"
    }
}

注意:我还使用了带子表达式的字符串扩展,而不是分配 $myPID 变量。