1

I'm trying to write a powershell tool control script that calls other ".EXE" programs using the ampersand operator. Example:

$cmd = ".\buffalo_wing.exe"

& $cmd "--really_hot" "--extra_spicy"

# ok now my command has failed by returning non-zero value...
#  but powershell keeps on going like the energizer bunny..

I tried setting:

$ErrorActionPreference="Stop"

But to no avail.

pico
  • 1,660
  • 4
  • 22
  • 52
  • PowerShell continues on because _it_ didn't fail. If you want to stop you could check the exit code of the last command yourself and then decide what to do based on that knowledge. – Matt May 01 '19 at 19:47
  • how does powershell know that it has failed? is it purely based on the integer return code from the program like an old dos or unix program? or is it some weird powershell command-let mechanism or object throw.. – pico May 01 '19 at 19:49
  • 2
    You can check the last exit code. Have a look here https://stackoverflow.com/questions/10666035/difference-between-and-lastexitcode-in-powershell – Matt May 01 '19 at 19:49
  • 1
    error code variable ($?) returns $false on failed ampersand command and $true on non-failing command. – pico May 01 '19 at 19:57

1 Answers1

0
$cmd = ".\buffalo_wing.exe"

& $cmd "--really_hot" "-o wing.log"
if ($? -eq $false) {
    write-host -background DarkBlue -foreground Red "<Error Exit>"
    exit 1 
}
pico
  • 1,660
  • 4
  • 22
  • 52