Basically I'm trying to return exit codes from start-process to the script so that MDT/SCCM can fail correctly if an installation fails.
Generically here is the code I'm using:
$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -Passthrough
Exit $proc.ExitCode
My question is when does the Start-Process
get executed? When I define $proc or when I call $proc.ExitCode
?
What I'm trying to do is use the exit code in a if
statement without having to store that code in another variable (reduce code clutter).
$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -PassThru
if ($proc.ExitCode -ne 0) {Exit $proc.ExitCode}
$proc2 = Start-Process -FilePath $setupexe2 -ArgumentList $setupargs2 -Wait -PassThru
if ($proc2.ExitCode -ne 0) {Exit $proc.ExitCode}
vs
$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -PassThru
$procexit = $proc.ExitCode
if ($procexit -ne 0) {Exit $procexit}
$proc2 = Start-Process -FilePath $setupexe2 -ArgumentList $setupargs2 -Wait -PassThru
$procexit2 - $proc2.ExitCode
if ($procexit2 -ne 0) {Exit $procexit2}
I don't want the Start-Process
to be called again just to kill the script and return the error code.
Start-Process
will start the process when you define $proc
and won't move on to the next line until it exits since you have the -Wait
parameter defined.
This line if ($proc.ExitCode -ne 0) {Exit $proc.ExitCode}
will not cause the code to run again.
You could test this on your computer by running the code with a quick program like notepad and see when program appears.
$a = start-process notepad.exe -wait -passthru
$a.exitcode