From 7f8422654e48235de11953b8af703f579442a34c Mon Sep 17 00:00:00 2001 From: Ben Hillis Date: Fri, 12 Dec 2025 14:16:47 -0800 Subject: [PATCH] test: fix and issue with the test-setup.ps1 script that was preventing it from being run from some version of powershell (#13834) Co-authored-by: Ben Hillis --- tools/test/test-setup.ps1 | 42 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/tools/test/test-setup.ps1 b/tools/test/test-setup.ps1 index 03fa804..939ab50 100644 --- a/tools/test/test-setup.ps1 +++ b/tools/test/test-setup.ps1 @@ -71,23 +71,41 @@ if ($Package) { try { if ($AllowUnsigned) { - # unfortunately -AllowUnsigned isn't supported on vb so we need to manually import the certificate and trust it. - (Get-AuthenticodeSignature $Package).SignerCertificate | Export-Certificate -FilePath private-wsl.cert | Out-Null - try - { - Import-Certificate -FilePath .\private-wsl.cert -CertStoreLocation Cert:\LocalMachine\Root | Out-Null + # Try to add with -AllowUnsigned first (supported in newer PowerShell) + try { + Add-AppxPackage $Package -AllowUnsigned -ErrorAction Stop } - finally - { - Remove-Item -Path .\private-wsl.cert + catch { + # Fallback: manually import the certificate and trust it + Write-Host "Attempting to import package certificate..." + $signature = Get-AuthenticodeSignature -LiteralPath $Package + if (-not $signature.SignerCertificate) { + Write-Error "Package is not signed or has no certificate. Cannot import certificate." + exit 1 + } + + $cert = $signature.SignerCertificate + $certPath = Join-Path $env:TEMP "wsl-package-cert.cer" + try { + $cert | Export-Certificate -FilePath $certPath | Out-Null + Import-Certificate -FilePath $certPath -CertStoreLocation Cert:\LocalMachine\Root | Out-Null + Write-Host "Certificate imported successfully. Retrying package installation..." + } + finally { + Remove-Item -Path $certPath -ErrorAction SilentlyContinue + } + + # Retry installation after importing certificate + Add-AppxPackage $Package -ErrorAction Stop } } - - Add-AppxPackage $Package + else { + Add-AppxPackage $Package -ErrorAction Stop + } } catch { - Write-Host $_ - Get-AppPackageLog -All + Write-Host "Error installing package: $_" + Get-AppPackageLog | Select-Object -First 64 | Format-List exit 1 } }