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:
@@ -100,6 +100,22 @@ Assert-Test 'Get-RdpRepairCertificateArray flattens nested Object[]' {
|
|||||||
if ($first.Thumbprint -ne 'NESTED01') { throw "expected NESTED01 got $($first.Thumbprint)" }
|
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' {
|
Assert-Test 'Get-RdpRepairDiagnosticSnapshot object shape' {
|
||||||
if (-not (Test-RdpRepairIsAdministrator)) {
|
if (-not (Test-RdpRepairIsAdministrator)) {
|
||||||
Write-Host ' (skip snapshot fields - not admin)' -ForegroundColor DarkYellow
|
Write-Host ' (skip snapshot fields - not admin)' -ForegroundColor DarkYellow
|
||||||
|
|||||||
@@ -93,12 +93,16 @@ function Get-RdpRepairCertificateArray {
|
|||||||
param([object]$Certificates)
|
param([object]$Certificates)
|
||||||
if ($null -eq $Certificates) { return @() }
|
if ($null -eq $Certificates) { return @() }
|
||||||
$flat = New-Object System.Collections.Generic.List[object]
|
$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[]]) {
|
if ($item -is [object[]]) {
|
||||||
foreach ($inner in @($item)) {
|
foreach ($inner in @($item)) { $pending.Push($inner) }
|
||||||
if ($null -ne $inner) { [void]$flat.Add($inner) }
|
} elseif ($null -ne $item.PSObject.Properties['Thumbprint']) {
|
||||||
}
|
[void]$flat.Add($item)
|
||||||
} elseif ($null -ne $item) {
|
} else {
|
||||||
[void]$flat.Add($item)
|
[void]$flat.Add($item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,7 +328,7 @@ function Export-RdpRepairDiagnosticReport {
|
|||||||
$snap.PSObject.Properties | ForEach-Object {
|
$snap.PSObject.Properties | ForEach-Object {
|
||||||
if ($_.Name -eq 'Certificates') {
|
if ($_.Name -eq 'Certificates') {
|
||||||
$lines += '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)
|
$lines += (' - {0}; Thumb={1}; NotAfter={2}; Key={3}' -f $c.Subject, $c.Thumbprint, $c.NotAfter, $c.HasKey)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user