Skip to content

Commit 831a86b

Browse files
authored
Enhance installation process for WinGet and PowerShell 7
Refactor PowerShell script to install WinGet and PowerShell 7 with improved error handling and fallback mechanisms.
1 parent 4ca489d commit 831a86b

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

Scripts/Start.ps1

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -146,58 +146,58 @@ if ($PSVersionTable.PSVersion.Major -lt 7) {
146146
New-Item -Path 'C:\WinDeploy\Download' -ItemType Directory -Force | Out-Null
147147
}
148148

149-
# Find or download installation scripts
150-
$wingetScript = Join-Path 'C:\WinDeploy\Download' "Install-Winget.ps1"
149+
# Install WinGet first
150+
Write-DeployLog "Installing WinGet..."
151+
try {
152+
# DevSkim: ignore DS104456 - Trusted source for WinGet installation
153+
Invoke-Expression "& { $(Invoke-RestMethod 'https://raw.githubusercontent.com/asheroto/winget-install/master/winget-install.ps1') }"
154+
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
155+
Start-Sleep -Seconds 2
156+
} catch {
157+
Write-Warning "WinGet install failed: $_"
158+
}
159+
160+
# Find PowerShell 7 installation script path
151161
$ps7Script = Join-Path 'C:\WinDeploy\Download' "Install-PowerShell7.ps1"
152162

153163
# Check local directory first
154164
if ($script:ScriptRoot) {
155-
$localWinget = Join-Path $script:ScriptRoot "Install-Winget.ps1"
156165
$localPs7 = Join-Path $script:ScriptRoot "Install-PowerShell7.ps1"
157-
if (Test-Path $localWinget) { $wingetScript = $localWinget }
158166
if (Test-Path $localPs7) { $ps7Script = $localPs7 }
159167
}
160168

161-
# Download if not found locally
162-
if (-not (Test-Path $wingetScript)) {
163-
try {
164-
$url = "https://raw.githubusercontent.com/Stensel8/WinDeploy/$($script:Version)/Scripts/Install-Winget.ps1"
165-
Invoke-WebRequest -Uri $url -OutFile $wingetScript -UseBasicParsing -ErrorAction Stop
166-
} catch {
167-
Write-Warning "Download failed: $_"
168-
}
169-
}
170-
171-
if (-not (Test-Path $ps7Script)) {
172-
try {
173-
$url = "https://raw.githubusercontent.com/Stensel8/WinDeploy/$($script:Version)/Scripts/Install-PowerShell7.ps1"
174-
Invoke-WebRequest -Uri $url -OutFile $ps7Script -UseBasicParsing -ErrorAction Stop
175-
} catch {
176-
Write-Warning "Download failed: $_"
177-
}
178-
}
179-
180-
# Install WinGet first
181-
if (Test-Path $wingetScript) {
182-
Write-DeployLog "Installing WinGet..."
183-
try {
184-
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $wingetScript
185-
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
186-
Start-Sleep -Seconds 2
187-
} catch {
188-
Write-Warning "WinGet install failed: $_"
189-
}
190-
}
191-
192169
# Install PowerShell 7
193-
if (Test-Path $ps7Script) {
194-
Write-DeployLog "Installing PowerShell 7..."
195-
try {
196-
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $ps7Script
170+
try {
171+
if (Get-Command winget -ErrorAction SilentlyContinue) {
172+
Write-DeployLog "Installing PowerShell 7 via WinGet..."
173+
& winget install --id Microsoft.PowerShell --source winget --accept-package-agreements --accept-source-agreements
197174
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
198175
Start-Sleep -Seconds 3
199-
} catch {
200-
Write-Warning "PowerShell 7 install failed: $_"
176+
} else {
177+
throw "WinGet not available for PS7"
178+
}
179+
} catch {
180+
Write-Warning "WinGet method failed for PS7: $_"
181+
# Fallback to script
182+
if (-not (Test-Path $ps7Script)) {
183+
try {
184+
$url = "https://aka.ms/install-powershell.ps1"
185+
Invoke-WebRequest -Uri $url -OutFile $ps7Script -UseBasicParsing -ErrorAction Stop
186+
} catch {
187+
Write-Warning "Download failed: $_"
188+
}
189+
}
190+
if (Test-Path $ps7Script) {
191+
Write-DeployLog "Installing PowerShell 7 via script..."
192+
try {
193+
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File $ps7Script
194+
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
195+
Start-Sleep -Seconds 3
196+
} catch {
197+
Write-Warning "PowerShell 7 script install failed: $_"
198+
}
199+
} else {
200+
Write-Warning "No installation method available for PowerShell 7"
201201
}
202202
}
203203
}

0 commit comments

Comments
 (0)