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 <benhill@ntdev.microsoft.com>
This commit is contained in:
Ben Hillis 2025-12-12 14:16:47 -08:00 committed by GitHub
parent 00b76b1d4b
commit 7f8422654e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 30 additions and 12 deletions

View File

@ -71,23 +71,41 @@ if ($Package) {
try { try {
if ($AllowUnsigned) if ($AllowUnsigned)
{ {
# unfortunately -AllowUnsigned isn't supported on vb so we need to manually import the certificate and trust it. # Try to add with -AllowUnsigned first (supported in newer PowerShell)
(Get-AuthenticodeSignature $Package).SignerCertificate | Export-Certificate -FilePath private-wsl.cert | Out-Null try {
try Add-AppxPackage $Package -AllowUnsigned -ErrorAction Stop
{
Import-Certificate -FilePath .\private-wsl.cert -CertStoreLocation Cert:\LocalMachine\Root | Out-Null
} }
finally catch {
{ # Fallback: manually import the certificate and trust it
Remove-Item -Path .\private-wsl.cert 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
} }
} }
else {
Add-AppxPackage $Package Add-AppxPackage $Package -ErrorAction Stop
}
} }
catch { catch {
Write-Host $_ Write-Host "Error installing package: $_"
Get-AppPackageLog -All Get-AppPackageLog | Select-Object -First 64 | Format-List
exit 1 exit 1
} }
} }