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 } }