I noticed a strange behaviour in Powershell. There are two .ps1 files:
main.ps1:
echo "running exit.ps1"
$myexitcode = & ".\exit.ps1" "hello" | select -Last 1
echo "Return code is $myexitcode"
echo "Exit code is $?"
echo "after running exit.ps1"
exit.ps1:
echo "processing args " $args
$script:_serverExitStatus = 9
#this is causing the program to exit completely
[System.Environment]::exit(3)
#BUT this is NOT causing the program to exit completely
#[System.Environment]::exit
With the above code, the process is getting completely exited and the output doesn't contain "after running exit.ps1". However, if I comment [System.Environment]::exit(3) and uncomment [System.Environment]::exit, output contains "after running exit.ps1"
I feel that exit must always exit completely. But, that's not the case with [System.Environment]::exit
What is the difference between these two things and which one is correct?
Version:
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 22000 832
Thank you.
[System.Environment]::Exitis a method, you need to invoke it for it to do anything :)