I want the script to find two identical processes that are currently running and pipe the PID of the process that matches a defined path in the variable $procName
by using the If($_.Path -eq $procName){
command.
After the correct process has been identified based on it's full file path found earlier, I want to use it's unique PID to check whether it is currently suspended or not suspended
.
If all goes well in the current pipe then use it's output value to run one of two commands below:
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"
}
My issue is the output is the same result no matter if the processes is currently suspended or not
Start-Process cmd.exe -WindowStyle Maximized -ArgumentList /D /K pssuspend.exe -nobanner -r 18960
This is what I have so far:
$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"
}
}
}
CodePudding user response:
Based on the code and respecting what Santiago has already pointed out, it looks like you are trying to take certain actions based on whether a particular process is responding. If I have that right, I don't see the need for Get-ChildItem
. You could append the Select-Object
command to include the Responding
property then do the logic like:
$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"
}
}
}
However, and unless there's more to the script, I don't think you need the Select-Object
command at all:
$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"
}
}
}
Also, nothing will be in the $procID
variable as the subsequent code doesn't write anything to the pipeline. I left it in only because I don't know the full intent.
You could also shorten the code Somewhat by moving some of the If Logic into a Where-Object
clause, See below with $ProcID
removed.
$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)"
}
}
Note: I also used string expansion with a subexpression instead of assigning the $myPID
variable.