I have a script that is working great, but I would like the addition of retrieving exit codes. My script uninstalls one program and installs another using the Start-Process cmdlet, but the exit code is not displaying. I read that there is a bug in the Start-Process cmdlet and that is why it is not displaying an exit code.
If you have a value in $InstallSCEP.ExitCode, then the first and third options will display the value. I should note that the third option would be preferred, since the first has unnecessary parenthesis. I took the liberty of trying out Start-Process and it’s parameters in PS 3.0 (on Server 2012) and PS 4.0 (on Windows 8.1).
PowerShell 3.0 (on Server 2012)
PS C:> $PSVersionTable.PSVersion.Major
3
PS C:> $x = Start-Process calc.exe -PassThru
PS C:> $x.ExitCode # While calc.exe is running
PS C:> # Nothing returned, as we’d suspect
PS C:> $x.ExitCode # After calc.exe is closed
PS C:> # Nothing returned –> have to use -Wait in PS 3.0
PS C:>
PS C:>
PS C:> $y = Start-Process calc.exe -PassThru -Wait
PS C:> $y.ExitCode
0
PS C:> Write-Warning -Message “Exit Code: $($y.ExitCode).”
WARNING: Exit Code: 0.
PowerShell 4.0 (on Windows 8.1)
PS C:> $PSVersionTable.PSVersion.Major
4
PS C:> $x = Start-Process calc.exe -PassThru
PS C:> $x.ExitCode # While calc.exe is running
PS C:> # Nothing returned, as we’d suspect
PS C:> $x.ExitCode # After calc.exe is closed
0
PS C:> # Wait is not required in PS 4.0
PS C:> Write-Warning -Message “Exit Code: $($x.ExitCode).”
WARNING: Exit Code: 0.