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>
Ben Hillis committed
Dec 12, 2025 at 14:16 UTC
7f8422654e48235de11953b8af703f579442a34c
1 file changed
+30
-12
tools/test/test-setup.ps1
+30
-12
@@ -71,23 +71,41 @@ if ($Package) {
71
try {
72
if ($AllowUnsigned)
73
{
74
- # unfortunately -AllowUnsigned isn't supported on vb so we need to manually import the certificate and trust it.
75
- (Get-AuthenticodeSignature $Package).SignerCertificate | Export-Certificate -FilePath private-wsl.cert | Out-Null
76
- try
77
- {
78
- Import-Certificate -FilePath .\private-wsl.cert -CertStoreLocation Cert:\LocalMachine\Root | Out-Null
74
+ # Try to add with -AllowUnsigned first (supported in newer PowerShell)
75
+ try {
76
+ Add-AppxPackage $Package -AllowUnsigned -ErrorAction Stop
77
}
80
- finally
81
- {
82
- Remove-Item -Path .\private-wsl.cert
78
+ catch {
79
+ # Fallback: manually import the certificate and trust it
80
+ Write-Host "Attempting to import package certificate..."
81
+ $signature = Get-AuthenticodeSignature -LiteralPath $Package
82
+ if (-not $signature.SignerCertificate) {
83
+ Write-Error "Package is not signed or has no certificate. Cannot import certificate."
84
+ exit 1
85
+ }
86
+
87
+ $cert = $signature.SignerCertificate
88
+ $certPath = Join-Path $env:TEMP "wsl-package-cert.cer"
89
+ try {
90
+ $cert | Export-Certificate -FilePath $certPath | Out-Null
91
+ Import-Certificate -FilePath $certPath -CertStoreLocation Cert:\LocalMachine\Root | Out-Null
92
+ Write-Host "Certificate imported successfully. Retrying package installation..."
93
+ }
94
+ finally {
95
+ Remove-Item -Path $certPath -ErrorAction SilentlyContinue
96
+ }
97
+
98
+ # Retry installation after importing certificate
99
+ Add-AppxPackage $Package -ErrorAction Stop
100
}
101
}
85
-
86
- Add-AppxPackage $Package
102
+ else {
103
+ Add-AppxPackage $Package -ErrorAction Stop
104
+ }
105
}
106
catch {
89
- Write-Host $_
90
- Get-AppPackageLog -All
107
+ Write-Host "Error installing package: $_"
108
+ Get-AppPackageLog | Select-Object -First 64 | Format-List
109
exit 1
110
}
111
}