From 572b36529581808bfdc627be9b1ae8d6e2f7777d Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 29 Sep 2017 17:36:03 -0700 Subject: [PATCH 1/4] [feature] removed test that is no longer valid due to change in -version behavior to return error --- test/powershell/Host/ConsoleHost.Tests.ps1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 930e33c00cc..025a02772ba 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -171,13 +171,6 @@ Describe "ConsoleHost unit tests" -tags "Feature" { $observed | should be $currentVersion } - It "-Version should ignore other parameters" { - $currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString() - $observed = & $powershell -version -command get-date - # no extraneous output - $observed | should be $currentVersion - } - It "-Version should write an error if a value is present" { $versionValue = "abrakadabra" $tempFile = Join-Path $testdrive "expectedError.txt" From 7d1f12f8c72801a14fee085e0fd300fbc1d6d078 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 29 Sep 2017 18:39:16 -0700 Subject: [PATCH 2/4] [feature] fixed `-v X` so that it errors out correctly with proper exit code updated test to catch this --- .../host/msh/CommandLineParameterParser.cs | 2 +- test/powershell/Host/ConsoleHost.Tests.ps1 | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 18632f22ab7..7f6b96b88b2 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -552,12 +552,12 @@ private void ParseHelper(string[] args) ++i; if (i < args.Length) { + _showVersion = false; WriteCommandLineError(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.DeprecatedVersionParameter,args[i])); } break; } - if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) { _showHelp = true; diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 025a02772ba..4cf66e810cd 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -173,11 +173,9 @@ Describe "ConsoleHost unit tests" -tags "Feature" { It "-Version should write an error if a value is present" { $versionValue = "abrakadabra" - $tempFile = Join-Path $testdrive "expectedError.txt" - $observed = & $powershell -version $versionValue > $tempFile - $expectedError = (Get-Content $tempFile)[0] - - $expectedError | Should Match $versionValue + $observed = & $powershell -version $versionValue 2>&1 + $observed | Should Match $versionValue + $LASTEXITCODE | Should Be $ExitCodeBadCommandLineParameter } It "-File should be default parameter" { From 863a28ebda97a1b45fb7e275f27e88b467f71431 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 30 Sep 2017 11:47:07 -0700 Subject: [PATCH 3/4] [feature] address PR feedback --- .../resources/CommandLineParameterParserStrings.resx | 2 +- test/powershell/Host/ConsoleHost.Tests.ps1 | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx index 19627b9271c..878c9fb5dc8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx @@ -273,6 +273,6 @@ EXAMPLES Invalid argument '{0}', did you mean: - Usage of '-Version {0}' is not supported. '-Version' currently only returns the current PowerShell version. + Unsupported value '{0}' passed to `-Version`. '-Version' only returns the current PowerShell version and must be used standalone. diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 4cf66e810cd..d3aea58dd0e 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -171,10 +171,13 @@ Describe "ConsoleHost unit tests" -tags "Feature" { $observed | should be $currentVersion } - It "-Version should write an error if a value is present" { - $versionValue = "abrakadabra" - $observed = & $powershell -version $versionValue 2>&1 - $observed | Should Match $versionValue + It "-Version should write an error if a value is present: " -TestCases @( + @{value="2"}, + @{value="-command 1-1"} + ) { + param($value) + $observed = & $powershell -version $value 2>&1 + $observed | Should Match $value $LASTEXITCODE | Should Be $ExitCodeBadCommandLineParameter } From 56028b3105d50068545c0df6cab8e1848d6ee63d Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 30 Sep 2017 12:58:08 -0700 Subject: [PATCH 4/4] [feature] make -v have behavior consistent with other tools like git, curl, bash where args after -v are ignored removed duplicate but not exactly help text that wasn't being used in the resource file removed parameters from help that are not support in PSCore6 --- .../host/msh/CommandLineParameterParser.cs | 20 +---- .../host/msh/ConsoleHost.cs | 2 +- .../CommandLineParameterParserStrings.resx | 86 ------------------- .../resources/ManagedEntranceStrings.resx | 24 ++---- test/powershell/Host/ConsoleHost.Tests.ps1 | 19 ++-- 5 files changed, 17 insertions(+), 134 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 7f6b96b88b2..ff02a8dbb29 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -400,15 +400,9 @@ internal bool NonInteractive private void ShowHelp() { + Dbg.Assert(_helpText != null, "_helpText should not be null"); _hostUI.WriteLine(""); - if (_helpText == null) - { - _hostUI.WriteLine(CommandLineParameterParserStrings.DefaultHelp); - } - else - { - _hostUI.Write(_helpText); - } + _hostUI.Write(_helpText); _hostUI.WriteLine(""); } @@ -548,17 +542,9 @@ private void ParseHelper(string[] args) _noInteractive = true; _skipUserInit = true; _noExit = false; - - ++i; - if (i < args.Length) - { - _showVersion = false; - WriteCommandLineError(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.DeprecatedVersionParameter,args[i])); - } break; } - - if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) + else if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) { _showHelp = true; _abortStartup = true; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 84e9cf33845..cb1bd8992ba 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -200,7 +200,7 @@ internal static int Start( // Alternatively, we could call s_theConsoleHost.UI.WriteLine(s_theConsoleHost.Version.ToString()); // or start up the engine and retrieve the information via $psversiontable.GitCommitId // but this returns the semantic version and avoids executing a script - s_theConsoleHost.UI.WriteLine("powershell " + PSVersionInfo.GitCommitId); + s_theConsoleHost.UI.WriteLine("PowerShell " + PSVersionInfo.GitCommitId); return 0; } diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx index 878c9fb5dc8..7ad30993383 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx @@ -129,89 +129,6 @@ Unrecognized parameter: '{0}'. - - PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>] - [-NoLogo] [-NoExit] [-NoProfile] [-NonInteractive] [-STA] - [-OutputFormat {Text | XML}] [-InputFormat {Text | XML}] - [-ConfigurationName <string>] - [-File fileName [arguments...]] [-ExecutionPolicy <ExecutionPolicy>] - [-Command { - | <script-block> [-args <arg-array>] - | <string> [<CommandParameters>] } ] - -PowerShell[.exe] -Help | -? | /? - --PSConsoleFile - Loads the specified PowerShell console file. To create a console - file, use Export-Console in PowerShell. - --Version - Starts the specified version of PowerShell. - --NoLogo - Hides the copyright banner at startup. - --NoExit - Does not exit after running startup commands. - --NoProfile - Does not use the user profile. - --NonInteractive - Does not present an interactive prompt to the user. - --STA - Uses a single-threaded apartment for the execution thread. - --OutputFormat - Determines how output from PowerShell is formatted. Valid values - are "Text" (text strings) or "XML" (serialized CLIXML format). - --InputFormat - Describes the format of data sent to PowerShell. Valid values are - "Text" (text strings) or "XML" (serialized CLIXML format). - --ConfigurationName - Specifies a configuration endpoint in which PowerShell is run. - This can be any endpoint registered on the local machine including the - default PowerShell remoting endpoints or a custom endpoint having - specific user role capabilities. - --Command - Executes the specified commands (and any parameters) as though they were - typed at the PowerShell command prompt, and then exits, unless - NoExit is specified. The value of Command can be "-", a string. or a - script block. - - If the value of Command is "-", the command text is read from standard - input. - - Script blocks must be enclosed in braces ({}). You can specify a script - block only when running PowerShell.exe in PowerShell. The results - of the script are returned to the parent shell as deserialized XML objects, - not live objects. - - If the value of Command is a string, Command must be the last parameter - in the command , because any characters typed after the command are - interpreted as the command arguments. - To write a string that runs a PowerShell command, use the format: - "& {<command>}" - where the quotation marks indicate a string and the invoke operator (&) - causes the command to be run. - --Help, -?, /? - Shows this message. If you are typing a PowerShell.exe command in Windows - PowerShell, prepend the command parameters with a hyphen (-), not a forward - slash (/). You can use either a hyphen or forward slash in Cmd.exe. - - -EXAMPLES - PowerShell -PSConsoleFile SqlSnapin.Psc1 - PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML - PowerShell -ConfigurationName AdminRoles - PowerShell -Command {Get-EventLog -LogName security} - PowerShell -Command "& {Get-EventLog -LogName security}" - - '-' was specified with the -Command parameter; no other arguments to -Command are permitted. @@ -272,7 +189,4 @@ EXAMPLES Invalid argument '{0}', did you mean: - - Unsupported value '{0}' passed to `-Version`. '-Version' only returns the current PowerShell version and must be used standalone. - diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx index 912656e069d..a40c9d4b1ce 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx @@ -122,8 +122,8 @@ Copyright (C) Microsoft Corporation. All rights reserved. - PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>] - [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive] [-Interactive] + PowerShell[.exe] [-Version] [-NoLogo] [-NoExit] + [-NoProfile] [-NonInteractive] [-Interactive] [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}] [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>] [-ConfigurationName <string>] @@ -133,13 +133,9 @@ Copyright (C) Microsoft Corporation. All rights reserved. PowerShell[.exe] -Help | -? | /? --PSConsoleFile - Loads the specified PowerShell console file. To create a console - file, use Export-Console in PowerShell. - -Version - Starts the specified version of PowerShell. - Enter a version number with the parameter, such as "-version 2.0". + Shows the version of PowerShell and exits. + Additional arguments are ignored. -NoLogo Hides the copyright banner at startup. @@ -147,13 +143,6 @@ PowerShell[.exe] -Help | -? | /? -NoExit Does not exit after running startup commands. --Sta - Starts the shell using a single-threaded apartment. - Single-threaded apartment (STA) is the default. - --Mta - Start the shell using a multithreaded apartment. - -NoProfile Does not load the PowerShell profile. @@ -228,8 +217,7 @@ PowerShell[.exe] -Help | -? | /? slash (/). You can use either a hyphen or forward slash in Cmd.exe. EXAMPLES - PowerShell -PSConsoleFile SqlSnapIn.Psc1 - PowerShell -version 2.0 -NoLogo -InputFormat text -OutputFormat XML + PowerShell -Version PowerShell -ConfigurationName AdminRoles PowerShell -Command {Get-EventLog -LogName security} PowerShell -Command "& {Get-EventLog -LogName security}" @@ -239,6 +227,6 @@ EXAMPLES $command = 'dir "c:\program files" ' $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) - powershell.exe -encodedCommand $encodedCommand + powershell -encodedCommand $encodedCommand diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index d3aea58dd0e..023e8e94f6f 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -165,20 +165,15 @@ Describe "ConsoleHost unit tests" -tags "Feature" { $actual | Should Be $expected } - It "-Version should return the engine version" { - $currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString() - $observed = & $powershell -version - $observed | should be $currentVersion - } - - It "-Version should write an error if a value is present: " -TestCases @( - @{value="2"}, - @{value="-command 1-1"} + It "-Version should return the engine version using: -version " -TestCases @( + @{value = ""}, + @{value = "2"}, + @{value = "-command 1-1"} ) { - param($value) + $currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString() $observed = & $powershell -version $value 2>&1 - $observed | Should Match $value - $LASTEXITCODE | Should Be $ExitCodeBadCommandLineParameter + $observed | should be $currentVersion + $LASTEXITCODE | Should Be 0 } It "-File should be default parameter" {