forked from PowerShell/PowerShell
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix accidental wrapping of scalar Write-Output input
when -NoEnumerate is used. Fix PowerShell#5122
- Loading branch information
Showing
2 changed files
with
30 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
--********************************************************************/ | ||
|
||
using System.Management.Automation; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.PowerShell.Commands | ||
{ | ||
|
@@ -14,15 +15,15 @@ namespace Microsoft.PowerShell.Commands | |
[Cmdlet(VerbsCommunications.Write, "Output", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113427", RemotingCapability = RemotingCapability.None)] | ||
public sealed class WriteOutputCommand : PSCmdlet | ||
{ | ||
private PSObject[] _inputObjects = null; | ||
private object _inputObjects = null; | ||
|
||
/// <summary> | ||
/// Holds the list of objects to be Written | ||
/// </summary> | ||
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromRemainingArguments = true)] | ||
[AllowNull] | ||
[AllowEmptyCollection] | ||
public PSObject[] InputObject | ||
public object InputObject | ||
{ | ||
get { return _inputObjects; } | ||
set { _inputObjects = value; } | ||
|
@@ -39,6 +40,25 @@ public SwitchParameter NoEnumerate | |
set; | ||
} | ||
|
||
/// <summary> | ||
/// This method implements the BeginProcessing method for Write-output command | ||
/// </summary> | ||
protected override void BeginProcessing() { | ||
// If the input is a List<object> instance with a single element, | ||
// assume that it is a single argument bound via ValueFromRemainingArguments and unwrap it. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mklement0
Author
Owner
|
||
// Note: | ||
// * This case is indistinguishable from something like the following: | ||
// Write-Output -NoEnumerate -InputObject ([System.Collections.Generic.List[object]]::new((, 1))) | ||
// However, this seems like an acceptable price to pay in order to prevent unexpected wrapping of | ||
// a scalar in a collection when using -NoEnumerate. | ||
// * Is the case of *multiple* ValueFromRemainingArguments values, the List<object> instance | ||
// is passed through when -NoEnumerate is specified. | ||
List<object> lst; | ||
if (_inputObjects is List<object> && (lst = (List<object>)_inputObjects).Count == 1) { | ||
_inputObjects = lst[0]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This method implements the ProcessRecord method for Write-output command | ||
/// </summary> | ||
|
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
This is not correct. The value might be an enumerable wrapped inside a PSObject in which case you have to extract the PSObject base object, then check for an enumeration.