Skip to content

Commit 5aee4ba

Browse files
iSazonovdaxian-dbw
authored andcommitted
Fix Select-Object to approve UX and fix return a property named * (PowerShell#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.
1 parent a2384bc commit 5aee4ba

2 files changed

Lines changed: 59 additions & 11 deletions

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,12 @@ private void ProcessExpressionParameter()
322322
new ParameterProcessor(new SelectObjectExpressionParameterDefinition());
323323
if ((Property != null) && (Property.Length != 0))
324324
{
325+
// Build property list taking into account the wildcards and @{name=;expression=}
325326
_propertyMshParameterList = processor.ProcessParameters(Property, invocationContext);
326327
}
327328
else
328329
{
330+
// Property don't exist
329331
_propertyMshParameterList = new List<MshParameter>();
330332
}
331333

@@ -337,6 +339,12 @@ private void ProcessExpressionParameter()
337339
if (ExcludeProperty != null)
338340
{
339341
_exclusionFilter = new MshExpressionFilter(ExcludeProperty);
342+
// ExcludeProperty implies -Property * for better UX
343+
if ((Property == null) || (Property.Length == 0))
344+
{
345+
Property = new Object[]{"*"};
346+
_propertyMshParameterList = processor.ProcessParameters(Property, invocationContext);
347+
}
340348
}
341349
}
342350

@@ -414,18 +422,10 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List<PSNoteP
414422
}
415423
}
416424

417-
if (expressionResults.Count == 0)
425+
// allow 'Select-Object -Property noexist-name' to return a PSObject with property noexist-name,
426+
// unless noexist-name itself contains wildcards
427+
if (expressionResults.Count == 0 && !ex.HasWildCardCharacters)
418428
{
419-
//Commented out for bug 1107600
420-
//if (!ex.HasWildCardCharacters)
421-
//{
422-
// ErrorRecord errorRecord = new ErrorRecord(
423-
// tracer.NewArgumentException("Property", ResourcesBaseName, "PropertyNotFound", ex.ToString()),
424-
// "PropertyNotFound",
425-
// ErrorCategory.InvalidArgument,
426-
// inputObject);
427-
// WriteError(errorRecord);
428-
//}
429429
expressionResults.Add(new MshExpressionResult(null, ex, null));
430430
}
431431

test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,51 @@ Describe "Select-Object DRT basic functionality" -Tags "CI" {
248248
$results[0] | Should Be "3"
249249
}
250250
}
251+
252+
Describe "Select-Object with Property = '*'" -Tags "CI" {
253+
254+
# Issue #2420
255+
It "Select-Object with implicit Property = '*' don't return property named '*'"{
256+
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
257+
$results.psobject.Properties.Item("*") | Should Be $null
258+
}
259+
260+
# Issue #2420
261+
It "Select-Object with explicit Property = '*' don't return property named '*'"{
262+
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
263+
$results.psobject.Properties.Item("*") | Should Be $null
264+
}
265+
266+
# Issue #2351
267+
It "Select-Object with implicit Property = '*' exclude single property"{
268+
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
269+
$results.psobject.Properties.Item("Thing") | Should Be $null
270+
$results.psobject.Properties.Item("*") | Should Be $null
271+
}
272+
273+
# Issue #2351
274+
It "Select-Object with explicit Property = '*' exclude single property"{
275+
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
276+
$results.psobject.Properties.Item("Thing") | Should Be $null
277+
$results.psobject.Properties.Item("*") | Should Be $null
278+
}
279+
280+
# Issue #2351
281+
It "Select-Object with implicit Property = '*' exclude not single property"{
282+
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -ExcludeProperty Param2
283+
$results.Param2 | Should Be $null
284+
$results.Thing | Should Be "thing1"
285+
}
286+
287+
# Issue #2351
288+
It "Select-Object with explicit Property = '*' exclude not single property"{
289+
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -Property * -ExcludeProperty Param2
290+
$results.Param2 | Should Be $null
291+
$results.Thing | Should Be "thing1"
292+
}
293+
294+
It "Select-Object with ExpandProperty and Property don't skip processing ExcludeProperty" {
295+
$p = Get-Process -Id $pid | Select-Object -Property Process* -ExcludeProperty ProcessorAffinity -ExpandProperty Modules
296+
$p[0].psobject.Properties.Item("ProcessorAffinity") | Should Be $null
297+
}
298+
}

0 commit comments

Comments
 (0)