Skip to content

Commit 0be829c

Browse files
fix(windows): fall back to ZIP install when MSI is not available for a release
Agent-Logs-Url: https://github.com/PSModule/Install-PowerShell/sessions/5033cf58-ed4c-4cc6-9396-0ed9bec18663 Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
1 parent b13a335 commit 0be829c

1 file changed

Lines changed: 69 additions & 37 deletions

File tree

scripts/windows/install.ps1

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ param()
66
Write-Host "Requested version: [$env:REQUESTED_VERSION]"
77
Write-Host "Prerelease: [$env:PRERELEASE]"
88

9+
# GitHub API headers used throughout the script
10+
$headers = @{
11+
'Accept' = 'application/vnd.github+json'
12+
'X-GitHub-Api-Version' = '2022-11-28'
13+
}
14+
if ($env:GITHUB_TOKEN) {
15+
$headers['Authorization'] = "Bearer $($env:GITHUB_TOKEN)"
16+
}
17+
$apiBase = if ($env:GH_HOST -and $env:GH_HOST -ne 'github.com') {
18+
"https://$($env:GH_HOST)/api/v3"
19+
} else {
20+
'https://api.github.com'
21+
}
22+
923
# Resolve 'latest' -> concrete version
1024
$req = $env:REQUESTED_VERSION
1125
if ($req -and $req.Trim().ToLower() -eq 'latest') {
12-
$headers = @{
13-
'Accept' = 'application/vnd.github+json'
14-
'X-GitHub-Api-Version' = '2022-11-28'
15-
}
16-
if ($env:GITHUB_TOKEN) {
17-
$headers['Authorization'] = "Bearer $($env:GITHUB_TOKEN)"
18-
}
19-
$apiBase = if ($env:GH_HOST -and $env:GH_HOST -ne 'github.com') {
20-
"https://$($env:GH_HOST)/api/v3"
21-
} else {
22-
'https://api.github.com'
23-
}
2426
if ($env:PRERELEASE -eq 'true') {
2527
$releases = Invoke-RestMethod -Uri "$apiBase/repos/PowerShell/PowerShell/releases?per_page=100" -Headers $headers
2628
$latestRelease = $releases | Where-Object { $_.prerelease -eq $true } | Select-Object -First 1
@@ -93,7 +95,7 @@ if ($isDowngrade) {
9395
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
9496
)
9597

96-
$isDetectedPreview = $detected -match '-preview|-rc'
98+
$isDetectedPreview = $detected -match '-'
9799
$pwshEntries = Get-ItemProperty -Path $regPaths -ErrorAction SilentlyContinue |
98100
Where-Object {
99101
$_.Publisher -eq 'Microsoft Corporation' -and
@@ -142,16 +144,37 @@ if ($isDowngrade) {
142144
}
143145
}
144146

145-
# Download requested MSI
146-
$msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
147-
$url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
147+
# Determine which package type is available for this release (MSI preferred, ZIP as fallback)
148+
$msiName = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
149+
$zipName = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.zip"
150+
$baseUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)"
151+
$useMsi = $true
152+
153+
try {
154+
$releaseAssets = (Invoke-RestMethod -Uri "$apiBase/repos/PowerShell/PowerShell/releases/tags/v$($env:REQUESTED_VERSION)" -Headers $headers).assets
155+
$assetNames = $releaseAssets | Select-Object -ExpandProperty name
156+
if ($assetNames -notcontains $msiName) {
157+
if ($assetNames -contains $zipName) {
158+
$useMsi = $false
159+
Write-Host "Note: No MSI package found for this release; using ZIP instead."
160+
} else {
161+
Write-Host "Error: No suitable Windows x64 package (MSI or ZIP) found for PowerShell $($env:REQUESTED_VERSION)."
162+
exit 1
163+
}
164+
}
165+
} catch {
166+
Write-Host "Warning: Could not query release assets; assuming MSI package is available."
167+
}
168+
169+
$pkg = if ($useMsi) { $msiName } else { $zipName }
170+
$url = "$baseUrl/$pkg"
148171
Write-Host "Downloading from: $url"
149172

150173
$downloadSucceeded = $false
151174
$maxAttempts = 3
152175
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
153176
try {
154-
$null = Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing -ErrorAction Stop
177+
$null = Invoke-WebRequest -Uri $url -OutFile $pkg -UseBasicParsing -ErrorAction Stop
155178
$downloadSucceeded = $true
156179
break
157180
} catch {
@@ -167,33 +190,42 @@ if (-not $downloadSucceeded) {
167190
exit 1
168191
}
169192

170-
# Install requested version
171-
Write-Host "Starting installation of PowerShell [$($env:REQUESTED_VERSION)]..."
172-
$msiProcess = Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait -PassThru
173-
if ($msiProcess.ExitCode -ne 0) {
174-
Write-Host "Error: Installation failed (exit code $($msiProcess.ExitCode))."
193+
# Compute the install directory (used for both MSI and ZIP installs, and for GITHUB_PATH)
194+
$isPrerelease = $env:REQUESTED_VERSION -match '-'
195+
$majorVersion = ($env:REQUESTED_VERSION -split '[.\-]')[0]
196+
if ($majorVersion -notmatch '^\d+$') {
197+
Write-Host "Warning: Computed major version ('$majorVersion') is invalid; skipping installation."
175198
exit 1
176199
}
200+
$installDir = if ($isPrerelease) {
201+
"$env:ProgramFiles\PowerShell\$majorVersion-preview"
202+
} else {
203+
"$env:ProgramFiles\PowerShell\$majorVersion"
204+
}
205+
206+
if ($useMsi) {
207+
# Install via MSI
208+
Write-Host "Starting installation of PowerShell [$($env:REQUESTED_VERSION)] from MSI..."
209+
$msiProcess = Start-Process msiexec.exe -ArgumentList '/i', $pkg, '/quiet', '/norestart' -Wait -PassThru
210+
if ($msiProcess.ExitCode -ne 0) {
211+
Write-Host "Error: Installation failed (exit code $($msiProcess.ExitCode))."
212+
exit 1
213+
}
214+
} else {
215+
# Install via ZIP extraction
216+
Write-Host "Starting installation of PowerShell [$($env:REQUESTED_VERSION)] from ZIP..."
217+
$null = New-Item -ItemType Directory -Force -Path $installDir
218+
Expand-Archive -Path $pkg -DestinationPath $installDir -Force
219+
}
177220

178221
Write-Host "Installation complete. PowerShell [$($env:REQUESTED_VERSION)] is now available."
179222

180223
# Add the install directory to GITHUB_PATH so subsequent `shell: pwsh` steps
181224
# resolve to the version we just installed - even for preview builds whose
182225
# install directory (7-preview) is not on the runner's default PATH.
183-
$isPrerelease = $env:REQUESTED_VERSION -match '-'
184-
$majorVersion = ($env:REQUESTED_VERSION -split '[.\-]')[0]
185-
if ($majorVersion -match '^\d+$') {
186-
$installDir = if ($isPrerelease) {
187-
"$env:ProgramFiles\PowerShell\$majorVersion-preview"
188-
} else {
189-
"$env:ProgramFiles\PowerShell\$majorVersion"
190-
}
191-
if (Test-Path $installDir) {
192-
Write-Host "Adding install directory to GITHUB_PATH: $installDir"
193-
Add-Content -Path $env:GITHUB_PATH -Value $installDir
194-
} else {
195-
Write-Host "Warning: Expected install directory not found: $installDir"
196-
}
226+
if (Test-Path $installDir) {
227+
Write-Host "Adding install directory to GITHUB_PATH: $installDir"
228+
Add-Content -Path $env:GITHUB_PATH -Value $installDir
197229
} else {
198-
Write-Host "Warning: Computed major version ('$majorVersion') is invalid; skipping GITHUB_PATH update."
230+
Write-Host "Warning: Expected install directory not found: $installDir"
199231
}

0 commit comments

Comments
 (0)