Powershell -command's exit code is not the same as a script's exit code -
i need run script powershell -command "& scriptname", , if exit code got powershell same exit code script returned. unfortunately, powershell returns 0 if script returns 0, , 1 if script returns non-zero value illustrated below:
ps c:\test> cat foo.ps1 exit 42 ps c:\test> ./foo.ps1 ps c:\test> echo $lastexitcode 42 ps c:\test> powershell -command "exit 42" ps c:\test> echo $lastexitcode 42 ps c:\test> powershell -command "& ./foo.ps1" ps c:\test> echo $lastexitcode 1 ps c:\test>
using [environment]::exit(42) works:
ps c:\test> cat .\baz.ps1 [environment]::exit(42) ps c:\test> powershell -command "& ./baz.ps1" ps c:\test> echo $lastexitcode 42 ps c:\test>
except when script run interactively, exits whole shell. suggestions?
if @ part sending -command script see never work. script running 'foo.ps1' script not have call exit, not return exit code.
if return exit code want. change " ', otherwise $lastexitcode resolved before 'send' string second powershell if run powershell.
ps c:\test> powershell -command './foo.ps1; exit $lastexitcode' ps c:\test> echo $lastexitcode 42
ps: check out -file parameter if want run script. know not return 1 if have terminating error -command does. see here more on last topic.
ps c:\test> powershell -file './foo.ps1' ps c:\test> echo $lastexitcode 42
Comments
Post a Comment