Execute-Process Try/Catch Doesn't Trap Errors

Greetings!

I like to use try/catch blocks to process errors. In v3.8.2, Execute-Process doesn’t seem to allow an error to be trapped. Instead, execution of the script stops and goes to the the outermost catch block. This doesn’t allow me to trap the error, deal with it and continue on in my script. Note that if I use -ContinueOnError $True, it has no affect on thrown behavior execution path. Your thoughts?

try { #Toolkit outer Try/Catch
try { #My Try/Catch
$ExitCodeInfo = Execute-Process -Path “$dirFiles\x32\setup.exe” -Parameters $theEXEParams -PassThru
}
catch {
# I want to do something here, but never gets called
If ( condition ) {
#Do something good
}
else {
throw #error for the Toolkit to catch thus causing graceful exit
}
}

 # More of my code

catch { #Toolkit outer catch block
#Toolkit code to process the error and exit the script
}

1 Like

ExitOnProcessFailure. Allows setting the script to immediately stop if the process fails and returns the exit code to the caller (e.g. SCCM) (Default: True)

Set it to false.

I have a similar but slightly different questions. I use -passthru to pass the return code to a variable and then do actions based on the return code. (I think I am using it correctly?) It is really useful with execute-msi.

From reading the changes, it seems that -passthru works differently in 3.8.2 and that I have lost the ability to do what I was doing with previous versions with both execute-process and execute-msi. If I have, how do I do it with 3.8.2. Is it the same answer as above? Set ExitOnProcessFailure to false? Do I have to ignore the return code that I want to check and do an action for?

Below are examples. Could you please provide examples of how it would be done with 3.8.2?

Execute-Process Example:
[psobject]$RetCode = Execute-process -Path “$envProgramFilesX86\SAP\SAPsetup\setup\NwSapSetup.exe” -Parameters ‘/Product:“SLC” /uninstall /nodlg /noRestart’ -PassThru
If ($RetCode.ExitCode -eq 129)
{
[int32]$mainExitCode = 3010
}

Execute-MSI Example:
[psobject]$RetCode = Execute-MSI -Action ‘Install’ -Path “Oracle Smart View 64-bit for Office.msi” -Parameters ‘/qb-! ALLUSERS=1 ROOTDRIVE=C:\ INSTALLDIR=C:\PROGRA~1\Oracle\SmartView’ -Passthru
If ($RetCode.ExitCode -eq 1641)
{
[int32]$mainExitCode = 3010
}

Thank you for your time.

1 Like

Previously -PassThru ignored all exit codes and returned an object with Exit Code, Standard Error output and Standard Output. In this version, the -PassThru parameter no longer ignores exit codes and is only here for returning the output object. So to achieve the same result as previously, you need to use -PassThru and -IgnoreExitCodes “*”

ExitOnProcessFailure is not needed here as it used if function fails or returned exit code is an error. I assume you want the script to terminate if it failed to launch the process and since the exit code is ignored, it is not needed for exit codes either.

1 Like