Skip to content

How to invoke command if multiple previous commands execute successfully? #21330

Discussion options

You must be logged in to vote

Given you are running external programs and have to check the $LastExitCode to detect an error then sum up the $LastExitcCde, it it is zero then there were no errors

#!/usr/bin/env pwsh

$ErrorActionPreference = "Stop"

trap
{
        throw $PSItem
}

$SumLastExitCode = 0

Write-Output "sudo zypper dup"

/usr/bin/true
$SumLastExitCode += $LastExitCode

Write-Output "flatpak update -y"
Write-Output "flatpak uninstall --unused"

/usr/bin/true
$SumLastExitCode += $LastExitCode

Write-Output "sudo snap refresh"

/usr/bin/true
$SumLastExitCode += $LastExitCode

if ( $SumLastExitCode -eq 0)
{
        Write-Output "Stop-Computer"
}

Change any of the /usr/bin/true to /usr/bin/false to see the ef…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
Answer selected by RokeJulianLockhart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants