Skip to content

Commit 2e901e5

Browse files
SteveL-MSFTiSazonov
authored andcommitted
fix powershell -version and help (PowerShell#4958)
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
1 parent a82f637 commit 2e901e5

File tree

5 files changed

+17
-140
lines changed

5 files changed

+17
-140
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,9 @@ internal bool NonInteractive
400400

401401
private void ShowHelp()
402402
{
403+
Dbg.Assert(_helpText != null, "_helpText should not be null");
403404
_hostUI.WriteLine("");
404-
if (_helpText == null)
405-
{
406-
_hostUI.WriteLine(CommandLineParameterParserStrings.DefaultHelp);
407-
}
408-
else
409-
{
410-
_hostUI.Write(_helpText);
411-
}
405+
_hostUI.Write(_helpText);
412406
_hostUI.WriteLine("");
413407
}
414408

@@ -548,17 +542,9 @@ private void ParseHelper(string[] args)
548542
_noInteractive = true;
549543
_skipUserInit = true;
550544
_noExit = false;
551-
552-
++i;
553-
if (i < args.Length)
554-
{
555-
WriteCommandLineError(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.DeprecatedVersionParameter,args[i]));
556-
}
557545
break;
558546
}
559-
560-
561-
if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?"))
547+
else if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?"))
562548
{
563549
_showHelp = true;
564550
_abortStartup = true;

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ internal static int Start(
200200
// Alternatively, we could call s_theConsoleHost.UI.WriteLine(s_theConsoleHost.Version.ToString());
201201
// or start up the engine and retrieve the information via $psversiontable.GitCommitId
202202
// but this returns the semantic version and avoids executing a script
203-
s_theConsoleHost.UI.WriteLine("powershell " + PSVersionInfo.GitCommitId);
203+
s_theConsoleHost.UI.WriteLine("PowerShell " + PSVersionInfo.GitCommitId);
204204
return 0;
205205
}
206206

src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -129,89 +129,6 @@
129129
<data name="UnknownParameter" xml:space="preserve">
130130
<value>Unrecognized parameter: '{0}'.</value>
131131
</data>
132-
<data name="DefaultHelp" xml:space="preserve">
133-
<value>PowerShell[.exe] [-PSConsoleFile &lt;file&gt; | -Version &lt;version&gt;]
134-
[-NoLogo] [-NoExit] [-NoProfile] [-NonInteractive] [-STA]
135-
[-OutputFormat {Text | XML}] [-InputFormat {Text | XML}]
136-
[-ConfigurationName &lt;string&gt;]
137-
[-File fileName [arguments...]] [-ExecutionPolicy &lt;ExecutionPolicy&gt;]
138-
[-Command { - | &lt;script-block&gt; [-args &lt;arg-array&gt;]
139-
| &lt;string&gt; [&lt;CommandParameters&gt;] } ]
140-
141-
PowerShell[.exe] -Help | -? | /?
142-
143-
-PSConsoleFile
144-
Loads the specified PowerShell console file. To create a console
145-
file, use Export-Console in PowerShell.
146-
147-
-Version
148-
Starts the specified version of PowerShell.
149-
150-
-NoLogo
151-
Hides the copyright banner at startup.
152-
153-
-NoExit
154-
Does not exit after running startup commands.
155-
156-
-NoProfile
157-
Does not use the user profile.
158-
159-
-NonInteractive
160-
Does not present an interactive prompt to the user.
161-
162-
-STA
163-
Uses a single-threaded apartment for the execution thread.
164-
165-
-OutputFormat
166-
Determines how output from PowerShell is formatted. Valid values
167-
are "Text" (text strings) or "XML" (serialized CLIXML format).
168-
169-
-InputFormat
170-
Describes the format of data sent to PowerShell. Valid values are
171-
"Text" (text strings) or "XML" (serialized CLIXML format).
172-
173-
-ConfigurationName
174-
Specifies a configuration endpoint in which PowerShell is run.
175-
This can be any endpoint registered on the local machine including the
176-
default PowerShell remoting endpoints or a custom endpoint having
177-
specific user role capabilities.
178-
179-
-Command
180-
Executes the specified commands (and any parameters) as though they were
181-
typed at the PowerShell command prompt, and then exits, unless
182-
NoExit is specified. The value of Command can be "-", a string. or a
183-
script block.
184-
185-
If the value of Command is "-", the command text is read from standard
186-
input.
187-
188-
Script blocks must be enclosed in braces ({}). You can specify a script
189-
block only when running PowerShell.exe in PowerShell. The results
190-
of the script are returned to the parent shell as deserialized XML objects,
191-
not live objects.
192-
193-
If the value of Command is a string, Command must be the last parameter
194-
in the command , because any characters typed after the command are
195-
interpreted as the command arguments.
196-
To write a string that runs a PowerShell command, use the format:
197-
"&amp; {&lt;command&gt;}"
198-
where the quotation marks indicate a string and the invoke operator (&amp;)
199-
causes the command to be run.
200-
201-
-Help, -?, /?
202-
Shows this message. If you are typing a PowerShell.exe command in Windows
203-
PowerShell, prepend the command parameters with a hyphen (-), not a forward
204-
slash (/). You can use either a hyphen or forward slash in Cmd.exe.
205-
206-
207-
EXAMPLES
208-
PowerShell -PSConsoleFile SqlSnapin.Psc1
209-
PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML
210-
PowerShell -ConfigurationName AdminRoles
211-
PowerShell -Command {Get-EventLog -LogName security}
212-
PowerShell -Command "&amp; {Get-EventLog -LogName security}"
213-
</value>
214-
</data>
215132
<data name="TooManyParametersToCommand" xml:space="preserve">
216133
<value>'-' was specified with the -Command parameter; no other arguments to -Command are permitted.</value>
217134
</data>
@@ -272,7 +189,4 @@ EXAMPLES
272189
<data name="InvalidArgument" xml:space="preserve">
273190
<value>Invalid argument '{0}', did you mean:</value>
274191
</data>
275-
<data name="DeprecatedVersionParameter" xml:space="preserve">
276-
<value>Usage of '-Version {0}' is not supported. '-Version' currently only returns the current PowerShell version.</value>
277-
</data>
278192
</root>

src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@
122122
Copyright (C) Microsoft Corporation. All rights reserved.</value>
123123
</data>
124124
<data name="ShellHelp" xml:space="preserve">
125-
<value>PowerShell[.exe] [-PSConsoleFile &lt;file&gt; | -Version &lt;version&gt;]
126-
[-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive] [-Interactive]
125+
<value>PowerShell[.exe] [-Version] [-NoLogo] [-NoExit]
126+
[-NoProfile] [-NonInteractive] [-Interactive]
127127
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
128128
[-WindowStyle &lt;style&gt;] [-EncodedCommand &lt;Base64EncodedCommand&gt;]
129129
[-ConfigurationName &lt;string&gt;]
@@ -133,27 +133,16 @@ Copyright (C) Microsoft Corporation. All rights reserved.</value>
133133

134134
PowerShell[.exe] -Help | -? | /?
135135

136-
-PSConsoleFile
137-
Loads the specified PowerShell console file. To create a console
138-
file, use Export-Console in PowerShell.
139-
140136
-Version
141-
Starts the specified version of PowerShell.
142-
Enter a version number with the parameter, such as "-version 2.0".
137+
Shows the version of PowerShell and exits.
138+
Additional arguments are ignored.
143139

144140
-NoLogo
145141
Hides the copyright banner at startup.
146142

147143
-NoExit
148144
Does not exit after running startup commands.
149145

150-
-Sta
151-
Starts the shell using a single-threaded apartment.
152-
Single-threaded apartment (STA) is the default.
153-
154-
-Mta
155-
Start the shell using a multithreaded apartment.
156-
157146
-NoProfile
158147
Does not load the PowerShell profile.
159148

@@ -228,8 +217,7 @@ PowerShell[.exe] -Help | -? | /?
228217
slash (/). You can use either a hyphen or forward slash in Cmd.exe.
229218

230219
EXAMPLES
231-
PowerShell -PSConsoleFile SqlSnapIn.Psc1
232-
PowerShell -version 2.0 -NoLogo -InputFormat text -OutputFormat XML
220+
PowerShell -Version
233221
PowerShell -ConfigurationName AdminRoles
234222
PowerShell -Command {Get-EventLog -LogName security}
235223
PowerShell -Command "&amp; {Get-EventLog -LogName security}"
@@ -239,6 +227,6 @@ EXAMPLES
239227
$command = 'dir "c:\program files" '
240228
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
241229
$encodedCommand = [Convert]::ToBase64String($bytes)
242-
powershell.exe -encodedCommand $encodedCommand</value>
230+
powershell -encodedCommand $encodedCommand</value>
243231
</data>
244232
</root>

test/powershell/Host/ConsoleHost.Tests.ps1

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,15 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
165165
$actual | Should Be $expected
166166
}
167167

168-
It "-Version should return the engine version" {
169-
$currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString()
170-
$observed = & $powershell -version
171-
$observed | should be $currentVersion
172-
}
173-
174-
It "-Version should ignore other parameters" {
168+
It "-Version should return the engine version using: -version <value>" -TestCases @(
169+
@{value = ""},
170+
@{value = "2"},
171+
@{value = "-command 1-1"}
172+
) {
175173
$currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString()
176-
$observed = & $powershell -version -command get-date
177-
# no extraneous output
174+
$observed = & $powershell -version $value 2>&1
178175
$observed | should be $currentVersion
179-
}
180-
181-
It "-Version should write an error if a value is present" {
182-
$versionValue = "abrakadabra"
183-
$tempFile = Join-Path $testdrive "expectedError.txt"
184-
$observed = & $powershell -version $versionValue > $tempFile
185-
$expectedError = (Get-Content $tempFile)[0]
186-
187-
$expectedError | Should Match $versionValue
176+
$LASTEXITCODE | Should Be 0
188177
}
189178

190179
It "-File should be default parameter" {

0 commit comments

Comments
 (0)