Add support to Get-Error to handle BoundParameters (#20640)

* Add support to `Get-Error` to handle BoundParameters

* sort the keys

* change to generalize for all dictionaries

* address Patrick's comment to generalize and use one codepath for dictionaries

* address codefactor
This commit is contained in:
Steve Lee
2024-08-26 19:25:16 -04:00
committed by GitHub
parent bb204e25b3
commit 9ceae0c544
2 changed files with 16 additions and 2 deletions
@@ -918,9 +918,9 @@ namespace System.Management.Automation.Runspaces
$null = $output.Append($prop.Value)
}
# Dictionary and Hashtable we want to show as Key/Value pairs, we don't do the extra whitespace alignment here
elseif ($prop.Value.GetType().Name.StartsWith('Dictionary') -or $prop.Value.GetType().Name -eq 'Hashtable') {
elseif ($prop.Value -is [System.Collections.IDictionary]) {
$isFirstElement = $true
foreach ($key in $prop.Value.Keys) {
foreach ($key in ($prop.Value.Keys | Sort-Object)) {
if ($isFirstElement) {
$null = $output.Append($newline)
}
@@ -155,4 +155,18 @@ Describe 'Get-Error tests' -Tag CI {
$out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String
$out | Should -Match "Message : Attempted to divide by zero."
}
It 'BoundParameters show as name/value pairs' {
try {
function test { [CmdletBinding()]param($A,$B) }
$Param = @{ A = "First"; B = "Second"; C = 24 }
test @Param
}
catch {
# do nothing
}
$out = Get-Error | Out-String
$out | Should -Match "BoundParameters\s+:\s+A : First\s+B : Second\s"
}
}