Add winget health check and reinstall before starting installs
Test-WingetHealth verifies winget --version and source list exit 0. If KO, Invoke-WingetReinstall removes the existing App Installer AppX and reinstalls from the provisioned package, then re-checks health. Reinstall requires admin rights; non-admin sessions exit with a clear error message. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -75,12 +75,104 @@ function Initialize-Winget {
|
||||
exit 1
|
||||
}
|
||||
|
||||
function Test-WingetHealth {
|
||||
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||
return $false
|
||||
}
|
||||
|
||||
& winget --version 2>&1 | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
return $false
|
||||
}
|
||||
|
||||
& winget source list 2>&1 | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
return $false
|
||||
}
|
||||
|
||||
return $true
|
||||
}
|
||||
|
||||
function Invoke-WingetReinstall {
|
||||
Write-Host "Removing existing App Installer..." -ForegroundColor Yellow
|
||||
|
||||
$AppInstaller = Get-AppxPackage -Name "Microsoft.DesktopAppInstaller" -ErrorAction SilentlyContinue
|
||||
if (-not $AppInstaller) {
|
||||
$AppInstaller = Get-AppxPackage -AllUsers -Name "Microsoft.DesktopAppInstaller" -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
if ($AppInstaller) {
|
||||
try {
|
||||
Remove-AppxPackage -Package $AppInstaller.PackageFullName -AllUsers -ErrorAction Stop
|
||||
Write-Host "App Installer removed." -ForegroundColor Green
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Could not remove App Installer: $_"
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "App Installer package not found, skipping removal." -ForegroundColor Gray
|
||||
}
|
||||
|
||||
$StagedPackage = Get-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.DisplayName -eq "Microsoft.DesktopAppInstaller" } |
|
||||
Select-Object -First 1
|
||||
|
||||
if (-not $StagedPackage) {
|
||||
Write-Error ("No provisioned App Installer package found.`n" +
|
||||
"Install App Installer from the Microsoft Store: " +
|
||||
"ms-windows-store://pdp/?productid=9nblggh4nns1")
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "Reinstalling App Installer from provisioned package..." -ForegroundColor Yellow
|
||||
|
||||
try {
|
||||
Add-AppxPackage -Path $StagedPackage.PackagePath -ErrorAction Stop
|
||||
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" +
|
||||
[System.Environment]::GetEnvironmentVariable("PATH", "User")
|
||||
Write-Host "App Installer reinstalled." -ForegroundColor Green
|
||||
}
|
||||
catch {
|
||||
Write-Error ("Failed to reinstall App Installer: $_`n" +
|
||||
"Install App Installer from the Microsoft Store: " +
|
||||
"ms-windows-store://pdp/?productid=9nblggh4nns1")
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
Initialize-Winget
|
||||
|
||||
$IsAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
|
||||
[Security.Principal.WindowsBuiltInRole]::Administrator
|
||||
)
|
||||
|
||||
Write-Host "Checking winget..." -ForegroundColor Cyan
|
||||
|
||||
if (-not (Test-WingetHealth)) {
|
||||
Write-Host "winget is not working correctly." -ForegroundColor Yellow
|
||||
|
||||
if (-not $IsAdministrator) {
|
||||
Write-Error "winget reinstall requires administrator rights. Run from an administrator terminal."
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "Attempting full reinstall..." -ForegroundColor Yellow
|
||||
Invoke-WingetReinstall
|
||||
|
||||
if (-not (Test-WingetHealth)) {
|
||||
Write-Error ("winget could not be restored after reinstall.`n" +
|
||||
"Install App Installer from the Microsoft Store: " +
|
||||
"ms-windows-store://pdp/?productid=9nblggh4nns1")
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "winget reinstalled and working." -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Host "winget OK." -ForegroundColor Green
|
||||
}
|
||||
|
||||
# --- Firefox installation functions ---
|
||||
|
||||
function Install-FirefoxEsr {
|
||||
|
||||
Reference in New Issue
Block a user