From 5aee4ba85ea9138a8bd01ee0fe5397cf34dfb655 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 1 Nov 2016 23:04:44 +0600 Subject: [PATCH] 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. --- .../commands/utility/select-object.cs | 22 ++++----- .../Select-Object.Tests.ps1 | 48 +++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs index be87e12510..334aac97d5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs @@ -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(); } @@ -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)); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 index 825cd4e9c2..bfceb195ec 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 @@ -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 + } +}