Using Execute-Process & monitor for exit code not 0

Hi all, I’ve used this toolkit for hundreds of packages over the years, a recent request from the business has me needing to do something a bit different, in short, I need to run a custom function I’ve written only if the exit code is not 0

In the install section I’m running:

Execute-Process -Path “$scriptDirectory\files\thor10-win\thor64.exe” -Parameters ‘blarr’

The good news is the exe handles exit codes, but I’m unsure how I can run my script only if the exit code is not 0, for example if it spits out 1 I’d like to run .\jason.ps1 if the exit code is 0 the toolkit should just process like normal

This is where you use the -passtru parameter.

If the examples in the PDF are not useful, give me a shout

Ahh that’s ideal for one of my issues, any exit code from the setup.exe will pass through to SCCM or MEM thank you.

Is there any variable that I can extract the exit code from? I had plans to write the last exit code to a registry location, as this software is a security scan, so ideally I’m keeping lots of logs for the audit.

$returnCode looks like it might be what I’m after ?

This should work for you:

#FYI: $dirFiles = = "$scriptDirectory\files"
$result = Execute-Process -Path "$dirFiles\thor10-win\thor64.exe" -Parameters 'blar' -ContinueOnError $true -ExitOnProcessFailure $false -PassThru
# $result now contains what happened after running the EXE. Regardless of success.
#you now have:
# $result.StdOut
# $result.StdErr
# $result.ExitCode

#Did it install OK?
If ($result.ExitCode -eq 0 ) {
    Write-Log "Thor 10 for Windows installed successfully" #and other future configurations
} Else {
    Write-Log "Thor 10 installation FAILED" #and other future configurations
}
1 Like

Ohh wow thankyou, will try soon

Legendary, I was able to tweak things a bit and add in

Set-RegistryKey -Key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Components\Thor2023’ -Name ‘LastExitCode’ -Value $result.ExitCode -Type String -ErrorAction Continue

Great success, great tool and community thankyou

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.