Fix Select-Object to approve UX and fix return a property named * (#2421)

There are mainly 2 changes:
1. When '-ExcludeProperty' is specified but '-Property' is not, use "*" as the default value for '-Property'.
2. Allow 'Select-Object -Property noexist-name' to return a PSObject with property noexist-name, unless noexist-name itself contains wildcards.
This commit is contained in:
iSazonov
2016-11-01 10:04:44 -07:00
committed by Dongbo Wang
parent a2384bc7d8
commit 5aee4ba85e
2 changed files with 59 additions and 11 deletions
@@ -322,10 +322,12 @@ namespace Microsoft.PowerShell.Commands
new ParameterProcessor(new SelectObjectExpressionParameterDefinition());
if ((Property != null) && (Property.Length != 0))
{
// Build property list taking into account the wildcards and @{name=;expression=}
_propertyMshParameterList = processor.ProcessParameters(Property, invocationContext);
}
else
{
// Property don't exist
_propertyMshParameterList = new List<MshParameter>();
}
@@ -337,6 +339,12 @@ namespace Microsoft.PowerShell.Commands
if (ExcludeProperty != null)
{
_exclusionFilter = new MshExpressionFilter(ExcludeProperty);
// ExcludeProperty implies -Property * for better UX
if ((Property == null) || (Property.Length == 0))
{
Property = new Object[]{"*"};
_propertyMshParameterList = processor.ProcessParameters(Property, invocationContext);
}
}
}
@@ -414,18 +422,10 @@ namespace Microsoft.PowerShell.Commands
}
}
if (expressionResults.Count == 0)
// allow 'Select-Object -Property noexist-name' to return a PSObject with property noexist-name,
// unless noexist-name itself contains wildcards
if (expressionResults.Count == 0 && !ex.HasWildCardCharacters)
{
//Commented out for bug 1107600
//if (!ex.HasWildCardCharacters)
//{
// ErrorRecord errorRecord = new ErrorRecord(
// tracer.NewArgumentException("Property", ResourcesBaseName, "PropertyNotFound", ex.ToString()),
// "PropertyNotFound",
// ErrorCategory.InvalidArgument,
// inputObject);
// WriteError(errorRecord);
//}
expressionResults.Add(new MshExpressionResult(null, ex, null));
}
@@ -248,3 +248,51 @@ Describe "Select-Object DRT basic functionality" -Tags "CI" {
$results[0] | Should Be "3"
}
}
Describe "Select-Object with Property = '*'" -Tags "CI" {
# Issue #2420
It "Select-Object with implicit Property = '*' don't return property named '*'"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2420
It "Select-Object with explicit Property = '*' don't return property named '*'"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with implicit Property = '*' exclude single property"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
$results.psobject.Properties.Item("Thing") | Should Be $null
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with explicit Property = '*' exclude single property"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
$results.psobject.Properties.Item("Thing") | Should Be $null
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with implicit Property = '*' exclude not single property"{
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -ExcludeProperty Param2
$results.Param2 | Should Be $null
$results.Thing | Should Be "thing1"
}
# Issue #2351
It "Select-Object with explicit Property = '*' exclude not single property"{
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -Property * -ExcludeProperty Param2
$results.Param2 | Should Be $null
$results.Thing | Should Be "thing1"
}
It "Select-Object with ExpandProperty and Property don't skip processing ExcludeProperty" {
$p = Get-Process -Id $pid | Select-Object -Property Process* -ExcludeProperty ProcessorAffinity -ExpandProperty Modules
$p[0].psobject.Properties.Item("ProcessorAffinity") | Should Be $null
}
}