Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.165
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.18362.0
Hi, I think I have been down this road before. Just after some assurance on what is the best practice for setting an exit level when exiting from a packaged console executable.
If I package a test script that simply contains ONE line...
exit 1
...and run it within a powershell CMD session, the exit level is not set. ie. equals 0
If I use the .net [environment]::exit(1), then it does get correctly set.
I've had issues in the past with correctly setting exits and I went away from using [environment]::exit(1) but can not recall why.
Is there a recommended or preferred method for exits?
Thanks
Recommended practice for setting exitlevel
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Recommended practice for setting exitlevel
Using $script:ExitCode variable is the preferred way of returning a value to a parent process. The "[Environment]::Exit("N")" call terminates the process at that exit location. No further clean up and termination routines (or any other code elements) are executed. It should only be used in grave error conditions. Best practices suggest that the exit of an application should always happen controlled at the same location.
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Recommended practice for setting exitlevel
Hi, thanks for the response but I fell into the trap of the most basic test...
Let's say we have a test.ps1 sitting in c:\temp and the only line in the script is;
$script:ExitCode=1
Then I run up a CMD window and run powershell then;
c:\temp\test.ps1
$lastexitcode
I would expect to see $lastexitcode return 1 but it returns 0
After some searching (soul and net), I find that the above command is interpreted as a command and not running a process therefore the exitcode expression is lost.
To correctly run you need to;
powershell -file c:\temp\test.ps1
It can be a bit confusing, especially when you have wrapper utilities sitting outside the test.ps1 that is controlling the launch.
Anyway, that works, thanks for the advice.
Regards
Let's say we have a test.ps1 sitting in c:\temp and the only line in the script is;
$script:ExitCode=1
Then I run up a CMD window and run powershell then;
c:\temp\test.ps1
$lastexitcode
I would expect to see $lastexitcode return 1 but it returns 0
After some searching (soul and net), I find that the above command is interpreted as a command and not running a process therefore the exitcode expression is lost.
To correctly run you need to;
powershell -file c:\temp\test.ps1
It can be a bit confusing, especially when you have wrapper utilities sitting outside the test.ps1 that is controlling the launch.
Anyway, that works, thanks for the advice.
Regards