An alternative to Start-Process
is the more syntactically terse call operator &
.
& cmd.exe /c 'ping.exe doesnotexist && exit 0 || exit 1'
The exit code will be contained in the built-in variable $LASTEXITCODE
so:
Write-Host $LASTEXITCODE
This will contain the exit code of the program run so you don't necessary have to run it with CMD.exe you could just do:
& ping.exe doesnotexist ; Write-Host $LASTEXITCODE
Applied to your command line program:
& cmd.exe /c 'D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini && exit 0 || exit 1'
Or just:
& D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini
In both cases $LASTEXITCODE
should be 0 for success, non-zero otherwise (if the external program was written correctly).
&&
works, and you can't chooseexit 0
orexit 1
after the setup runs. You have to testERRORLEVEL
, and that's typically done in a batch file, which could then exit with the appropriate error code.&&
runs the left side and if it's false (the command failed) then it runs the right side of the&&
. Operator precedence groups(Left && Middle) || Right
. Now that I think about it, if||
is a conditional-or and only runs until a true value, then I should have actually used&
instead of||
.