Skip to content

Commit

Permalink
fix powershell -version and help (#4958)
Browse files Browse the repository at this point in the history
 PowerShell -v  behavior updated to align with other tools like git, curl, and bash where args after  -v  are silently ignored.

Built-in help updated to reflect changes we've made to the console host in PSCore6 removing unsupported parameters.

* [feature]
removed test that is no longer valid due to change in -version behavior to return error

* [feature]
fixed `-v X` so that it errors out correctly with proper exit code
updated test to catch this

* [feature]
address PR feedback

* [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
  • Loading branch information
SteveL-MSFT authored and iSazonov committed Oct 1, 2017
1 parent a82f637 commit 2e901e5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 140 deletions.
Expand Up @@ -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("");
}

Expand Down Expand Up @@ -548,17 +542,9 @@ private void ParseHelper(string[] args)
_noInteractive = true;
_skipUserInit = true;
_noExit = false;

++i;
if (i < args.Length)
{
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;
Expand Down
Expand Up @@ -200,7 +200,7 @@ internal sealed partial class ConsoleHost
// 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;
}

Expand Down
Expand Up @@ -129,89 +129,6 @@
<data name="UnknownParameter" xml:space="preserve">
<value>Unrecognized parameter: '{0}'.</value>
</data>
<data name="DefaultHelp" xml:space="preserve">
<value>PowerShell[.exe] [-PSConsoleFile &lt;file&gt; | -Version &lt;version&gt;]
[-NoLogo] [-NoExit] [-NoProfile] [-NonInteractive] [-STA]
[-OutputFormat {Text | XML}] [-InputFormat {Text | XML}]
[-ConfigurationName &lt;string&gt;]
[-File fileName [arguments...]] [-ExecutionPolicy &lt;ExecutionPolicy&gt;]
[-Command { - | &lt;script-block&gt; [-args &lt;arg-array&gt;]
| &lt;string&gt; [&lt;CommandParameters&gt;] } ]

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:
"&amp; {&lt;command&gt;}"
where the quotation marks indicate a string and the invoke operator (&amp;)
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 "&amp; {Get-EventLog -LogName security}"
</value>
</data>
<data name="TooManyParametersToCommand" xml:space="preserve">
<value>'-' was specified with the -Command parameter; no other arguments to -Command are permitted.</value>
</data>
Expand Down Expand Up @@ -272,7 +189,4 @@ EXAMPLES
<data name="InvalidArgument" xml:space="preserve">
<value>Invalid argument '{0}', did you mean:</value>
</data>
<data name="DeprecatedVersionParameter" xml:space="preserve">
<value>Usage of '-Version {0}' is not supported. '-Version' currently only returns the current PowerShell version.</value>
</data>
</root>
Expand Up @@ -122,8 +122,8 @@
Copyright (C) Microsoft Corporation. All rights reserved.</value>
</data>
<data name="ShellHelp" xml:space="preserve">
<value>PowerShell[.exe] [-PSConsoleFile &lt;file&gt; | -Version &lt;version&gt;]
[-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive] [-Interactive]
<value>PowerShell[.exe] [-Version] [-NoLogo] [-NoExit]
[-NoProfile] [-NonInteractive] [-Interactive]
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
[-WindowStyle &lt;style&gt;] [-EncodedCommand &lt;Base64EncodedCommand&gt;]
[-ConfigurationName &lt;string&gt;]
Expand All @@ -133,27 +133,16 @@ Copyright (C) Microsoft Corporation. All rights reserved.</value>

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.

-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.

Expand Down Expand Up @@ -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 "&amp; {Get-EventLog -LogName security}"
Expand All @@ -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</value>
powershell -encodedCommand $encodedCommand</value>
</data>
</root>
25 changes: 7 additions & 18 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Expand Up @@ -165,26 +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 ignore other parameters" {
It "-Version should return the engine version using: -version <value>" -TestCases @(
@{value = ""},
@{value = "2"},
@{value = "-command 1-1"}
) {
$currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString()
$observed = & $powershell -version -command get-date
# no extraneous output
$observed = & $powershell -version $value 2>&1
$observed | should be $currentVersion
}

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
$LASTEXITCODE | Should Be 0
}

It "-File should be default parameter" {
Expand Down

0 comments on commit 2e901e5

Please sign in to comment.