lastexitcode

zosrothko's Avatar

zosrothko

05 Feb, 2018 05:30 PM

Hi

Why the snippet below display only: lastexitcode=

  if ($lastexitcode -ne 0)  { 
       Write-Host -ForegroundColor Yellow 'lastexitcode=' $lastexitcode;
       exit $lastexitcode 
  }
  1. 1 Posted by Ilya Finkelshte... on 05 Feb, 2018 10:01 PM

    Ilya Finkelshteyn's Avatar

    As discussed here, $lastexitcode is being set by legacy executables, while PowerShell does not set it. You can check this even on your computer by typing something like $LASTEXITCODE=$null; bla; $LASTEXITCODE and then $LASTEXITCODE=$null; ping; $LASTEXITCODE. As you see bla does not generate exit code while ping does.

    If you need script to fail on any error, simple set $ErrorActionPreference = "Stop" in the beginning of the script.

  2. 2 Posted by zosrothko on 06 Feb, 2018 07:10 AM

    zosrothko's Avatar

    This is your answer from my previous question:
    "PowerShell script is happy as long as last command is happy. To avoid those failed positives in the future, use either cmd, or check exit code after every command (or only commands which most probably can fail) in PowerShell (something like if ($lastexitcode -ne 0){ exit $lastexitcode })."

    So, what is the proper way to check a compile error status?

  3. 3 Posted by Ilya Finkelshte... on 06 Feb, 2018 05:11 PM

    Ilya Finkelshteyn's Avatar

    Sorry, I myself realized that $lastexitcode approach is not universal, just recently :( If you need to check for errors in specific places in your script, use S? variable (difference here). Or use $ErrorActionPreference = "Stop" in the top of the script to make the whole script fail in case of any error. Sorry once again for misleading earlier.

  4. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:26 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac