fix: Export-RdpRepairDiagnosticReport ломался на одном cert (foreach PS 5.1)

foreach по PSCustomObject перечисляет свойства, не cert — нужен @().
Рекурсивное flatten в Get-RdpRepairCertificateArray, самотест Export-паттерна.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
PTah
2026-06-12 20:32:38 +10:00
parent 5761710fa1
commit d25fea82b1
2 changed files with 26 additions and 6 deletions
+16
View File
@@ -100,6 +100,22 @@ Assert-Test 'Get-RdpRepairCertificateArray flattens nested Object[]' {
if ($first.Thumbprint -ne 'NESTED01') { throw "expected NESTED01 got $($first.Thumbprint)" }
}
Assert-Test 'foreach must use @() wrapper on single cert (Export pattern)' {
$mock = [PSCustomObject]@{
Thumbprint = 'LOOP01'
Subject = 'CN=loop'
NotAfter = (Get-Date).AddDays(1)
HasKey = $true
}
$lines = @()
foreach ($c in @(Get-RdpRepairCertificateArray @($mock))) {
$lines += $c.Subject
}
if ($lines.Count -ne 1 -or $lines[0] -ne 'CN=loop') {
throw "expected CN=loop got $($lines -join ',')"
}
}
Assert-Test 'Get-RdpRepairDiagnosticSnapshot object shape' {
if (-not (Test-RdpRepairIsAdministrator)) {
Write-Host ' (skip snapshot fields - not admin)' -ForegroundColor DarkYellow
+10 -6
View File
@@ -93,12 +93,16 @@ function Get-RdpRepairCertificateArray {
param([object]$Certificates)
if ($null -eq $Certificates) { return @() }
$flat = New-Object System.Collections.Generic.List[object]
foreach ($item in @($Certificates)) {
$pending = New-Object System.Collections.Generic.Stack[object]
foreach ($entry in @($Certificates)) { $pending.Push($entry) }
while ($pending.Count -gt 0) {
$item = $pending.Pop()
if ($null -eq $item) { continue }
if ($item -is [object[]]) {
foreach ($inner in @($item)) {
if ($null -ne $inner) { [void]$flat.Add($inner) }
}
} elseif ($null -ne $item) {
foreach ($inner in @($item)) { $pending.Push($inner) }
} elseif ($null -ne $item.PSObject.Properties['Thumbprint']) {
[void]$flat.Add($item)
} else {
[void]$flat.Add($item)
}
}
@@ -324,7 +328,7 @@ function Export-RdpRepairDiagnosticReport {
$snap.PSObject.Properties | ForEach-Object {
if ($_.Name -eq 'Certificates') {
$lines += 'Certificates:'
foreach ($c in (Get-RdpRepairCertificateArray $_.Value)) {
foreach ($c in @(Get-RdpRepairCertificateArray $_.Value)) {
$lines += (' - {0}; Thumb={1}; NotAfter={2}; Key={3}' -f $c.Subject, $c.Thumbprint, $c.NotAfter, $c.HasKey)
}
} else {