-
-
Save chrisoldwood/bbd6aac19bac2ff0c54ba32a705c939b to your computer and use it in GitHub Desktop.
exit 1 |
param( | |
[Parameter(Position=0,Mandatory=1)] | |
[string]$UnpassedMandatoryParameter | |
) |
$ErrorActionPreference = 'stop' | |
Get-ChildItem C:\UnknownFolder\UnknownFile |
throw 'error!' |
@echo off | |
setlocal | |
set Version=%~1 | |
echo. | |
echo All tests should return a non-zero exit code | |
echo To target a specific version use: TestPowerShellExitCode "-Version X" | |
echo (-Version 1 and -Version 2 are known to be broken) | |
echo. | |
PowerShell %Version% -File PowerShellExitTest.ps1 1>nul 2>&1 | |
echo TEST "-File & Exit": %errorlevel% | |
PowerShell %Version% -File PowerShellThrowTest.ps1 1>nul 2>&1 | |
echo TEST "-File & Throw": %errorlevel% | |
PowerShell %Version% -File PowerShellStopTest.ps1 1>nul 2>&1 | |
echo TEST "-File & Stop": %errorlevel% | |
PowerShell %Version% -NonInteractive -File PowerShellParamTest.ps1 1>nul 2>&1 | |
echo TEST "-File & Param": %errorlevel% | |
PowerShell %Version% -Command "exit 1" 1>nul 2>&1 | |
echo TEST "-Command & Exit": %errorlevel% | |
PowerShell %Version% -Command "throw 'error!'" 1>nul 2>&1 | |
echo TEST "-Command & Throw": %errorlevel% | |
PowerShell %Version% -Command "$ErrorActionPreference='stop';Get-ChildItem C:\UnknownFolder\UnknownFile" 1>nul 2>&1 | |
echo TEST "-Command & Stop": %errorlevel% |
Recently I've found out that -version
option has some serious limitations. Some background information
is here.
Basically, it can run only two versions of Powershell: the latest installed version and version 2. Instead of
all others one always gets the latest.
The behavior is the same on all current Windows versions. The difference is only in which versions of Windows
Management Framework can be installed.
Here are test results on Windows Server 2012.
- By default it runs version 3:
>powershell $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
And we can run version 2 (provided .NET Framework v2.0.50727 is installed):
>powershell -version 2 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
-version 3
option, as expected, gives us version 3:
>powershell -version 3 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
- Let's install Windows Management Framework 4.0.
By default we get version 4:
>powershell $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
-version 2
option also works as expected:
>powershell -version 2 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
With -version 3
option comes a surprise - we get version 4 instead:
>powershell -version 3 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
Now with -version 4
option:
>powershell -version 4 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
- And finally with Windows Management Framework 5.0 installed.
Version 5 by default:
>powershell $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
-version 2
and -version 5
give us versions 2 and 5 respectively:
>powershell -version 2 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
>powershell -version 5 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
Whereas both -version 3
and version 4
give us version 5:
>powershell -version 3 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
>powershell -version 4 $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
I've reproduced the problem with PowerShell 4, but it works fine with PowerShell 5 on Windows Server 2012 R2