lastexitcode
Hi
Why the snippet below display only: lastexitcode=
if ($lastexitcode -ne 0) {
Write-Host -ForegroundColor Yellow 'lastexitcode=' $lastexitcode;
exit $lastexitcode
}
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

1 Posted by Ilya Finkelshte... on 05 Feb, 2018 10:01 PM
As discussed here,
$lastexitcodeis 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; $LASTEXITCODEand then$LASTEXITCODE=$null; ping; $LASTEXITCODE. As you seebladoes not generate exit code whilepingdoes.If you need script to fail on any error, simple set
$ErrorActionPreference = "Stop"in the beginning of the script.2 Posted by zosrothko on 06 Feb, 2018 07:10 AM
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 Posted by Ilya Finkelshte... on 06 Feb, 2018 05:11 PM
Sorry, I myself realized that
$lastexitcodeapproach is not universal, just recently :( If you need to check for errors in specific places in your script, useS?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.Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:26 AM.