From a77df36f8b2ff6ac4fbadd201a4ec76b86382c08 Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 24 May 2026 11:47:40 +0200 Subject: [PATCH] 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 --- powershell/libres-softwares-install.ps1 | 92 +++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/powershell/libres-softwares-install.ps1 b/powershell/libres-softwares-install.ps1 index bee290f..a1e876f 100644 --- a/powershell/libres-softwares-install.ps1 +++ b/powershell/libres-softwares-install.ps1 @@ -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 {