Add ValidateNotNullOrEmpty attribute to the -Property of Format-Table/List/Custom (#26552)

This commit is contained in:
Dongbo Wang
2026-01-12 10:47:41 -08:00
committed by GitHub
parent c792b3c0ba
commit 9bc47950e2
3 changed files with 16 additions and 0 deletions
@@ -32,6 +32,7 @@ namespace Microsoft.PowerShell.Commands
/// will be determined using property sets, etc.
/// </summary>
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public object[] Property
{
get { return _props; }
@@ -728,6 +728,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// will be determined using property sets, etc.
/// </summary>
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public object[] Property { get; set; }
/// <summary>
@@ -63,3 +63,17 @@ Describe "Hidden properties should not be returned by the 'FirstOrDefault' primi
$outstring.Trim() | Should -BeExactly "Param$([System.Environment]::NewLine)-----$([System.Environment]::NewLine)Foo"
}
}
Describe "'Format-Table/List/Custom -Property' should not throw NullRef exception" -Tag CI {
It "'<Command> -Property' requires value to be not null and not empty" -TestCases @(
@{ Command = "Format-Table"; NameInErrorId = "FormatTableCommand" }
@{ Command = "Format-List"; NameInErrorId = "FormatListCommand" }
@{ Command = "Format-Custom"; NameInErrorId = "FormatCustomCommand" }
) {
param($Command, $NameInErrorId)
{ Get-Process -Id $PID | & $Command -Property @() } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.$NameInErrorId"
{ Get-Process -Id $PID | & $Command -Property $null } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.$NameInErrorId"
}
}