Skip to content

Commit 9ceae0c

Browse files
authored
Add support to Get-Error to handle BoundParameters (PowerShell#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
1 parent bb204e2 commit 9ceae0c

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,9 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
918918
$null = $output.Append($prop.Value)
919919
}
920920
# Dictionary and Hashtable we want to show as Key/Value pairs, we don't do the extra whitespace alignment here
921-
elseif ($prop.Value.GetType().Name.StartsWith('Dictionary') -or $prop.Value.GetType().Name -eq 'Hashtable') {
921+
elseif ($prop.Value -is [System.Collections.IDictionary]) {
922922
$isFirstElement = $true
923-
foreach ($key in $prop.Value.Keys) {
923+
foreach ($key in ($prop.Value.Keys | Sort-Object)) {
924924
if ($isFirstElement) {
925925
$null = $output.Append($newline)
926926
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Error.Tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,18 @@ Describe 'Get-Error tests' -Tag CI {
155155
$out = pwsh -noprofile -command 'Set-StrictMode -Version Latest; $PSStyle.OutputRendering = "PlainText"; 1/0; Get-Error' | Out-String
156156
$out | Should -Match "Message : Attempted to divide by zero."
157157
}
158+
159+
It 'BoundParameters show as name/value pairs' {
160+
try {
161+
function test { [CmdletBinding()]param($A,$B) }
162+
$Param = @{ A = "First"; B = "Second"; C = 24 }
163+
test @Param
164+
}
165+
catch {
166+
# do nothing
167+
}
168+
169+
$out = Get-Error | Out-String
170+
$out | Should -Match "BoundParameters\s+:\s+A : First\s+B : Second\s"
171+
}
158172
}

0 commit comments

Comments
 (0)