Last active
July 22, 2024 04:12
-
-
Save chrisoldwood/bbd6aac19bac2ff0c54ba32a705c939b to your computer and use it in GitHub Desktop.
A set of tests to show if your version of PowerShell returns the correct exit code under various error conditions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Position=0,Mandatory=1)] | |
[string]$UnpassedMandatoryParameter | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = 'stop' | |
Get-ChildItem C:\UnknownFolder\UnknownFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
throw 'error!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Recently I've found out that
-version
option has some serious limitations. Some background informationis 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.
And we can run version 2 (provided .NET Framework v2.0.50727 is installed):
-version 3
option, as expected, gives us version 3:By default we get version 4:
-version 2
option also works as expected:With
-version 3
option comes a surprise - we get version 4 instead:Now with
-version 4
option:Version 5 by default:
-version 2
and-version 5
give us versions 2 and 5 respectively:Whereas both
-version 3
andversion 4
give us version 5: