pawelkl

repair winget

Aug 14th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Install-WinGet {
  2.   $tempFolderName = 'WinGetInstall'
  3.   $tempFolder = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath $tempFolderName
  4.   New-Item $tempFolder -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
  5.  
  6.   $apiLatestUrl = if ($Prerelease) { 'https://api.github.com/repos/microsoft/winget-cli/releases?per_page=1' } else { 'https://api.github.com/repos/microsoft/winget-cli/releases/latest' }
  7.   [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  8.   $WebClient = New-Object System.Net.WebClient
  9.  
  10.   function Get-LatestUrl    {
  11.         ((Invoke-WebRequest $apiLatestUrl -UseBasicParsing | ConvertFrom-Json).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle$' }).browser_download_url
  12.   }
  13.  
  14.   function Get-LatestHash {
  15.     $shaUrl = ((Invoke-WebRequest $apiLatestUrl -UseBasicParsing | ConvertFrom-Json).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt$' }).browser_download_url
  16.     $shaFile = Join-Path -Path $tempFolder -ChildPath 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt'
  17.     $WebClient.DownloadFile($shaUrl, $shaFile)
  18.     Get-Content $shaFile
  19.   }
  20.   $desktopAppInstaller = @{
  21.     fileName = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle'
  22.     url      = $(Get-LatestUrl)
  23.     hash     = $(Get-LatestHash)
  24.   }
  25.   $vcLibsUwp = @{
  26.     fileName = 'Microsoft.VCLibs.x64.14.00.Desktop.appx'
  27.     url      = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
  28.     hash     = '9BFDE6CFCC530EF073AB4BC9C4817575F63BE1251DD75AAA58CB89299697A569'
  29.   }
  30.   $uiLibsUwp = @{
  31.     fileName = 'Microsoft.UI.Xaml.2.7.zip'
  32.     url      = 'https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.0'
  33.     hash     = '422FD24B231E87A842C4DAEABC6A335112E0D35B86FAC91F5CE7CF327E36A591'
  34.   }
  35.   $dependencies = @($desktopAppInstaller, $vcLibsUwp, $uiLibsUwp)
  36.   Write-Host '--> Checking dependencies'
  37.   foreach ($dependency in $dependencies) {
  38.     $dependency.file = Join-Path -Path $tempFolder -ChildPath $dependency.fileName
  39.     if (-Not ((Test-Path -Path $dependency.file -PathType Leaf) -And $dependency.hash -eq $(Get-FileHash $dependency.file).Hash)) {
  40.       Write-Host @"
  41.    - Downloading:
  42.      $($dependency.url)
  43. "@
  44.       try {
  45.         $WebClient.DownloadFile($dependency.url, $dependency.file)
  46.       }
  47.       catch {
  48.         #Pass the exception as an inner exception
  49.         throw [System.Net.WebException]::new("Error downloading $($dependency.url).", $_.Exception)
  50.       }
  51.       if (-not ($dependency.hash -eq $(Get-FileHash $dependency.file).Hash)) {
  52.         throw [System.Activities.VersionMismatchException]::new('Dependency hash does not match the downloaded file')
  53.       }
  54.     }
  55.   }
  56.  
  57.   if (-Not (Test-Path (Join-Path -Path $tempFolder -ChildPath \Microsoft.UI.Xaml.2.7\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx)))    {
  58.     Expand-Archive -Path $uiLibsUwp.file -DestinationPath ($tempFolder + '\Microsoft.UI.Xaml.2.7') -Force
  59.   }
  60.   $uiLibsUwp.file = (Join-Path -Path $tempFolder -ChildPath \Microsoft.UI.Xaml.2.7\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx)
  61.   Add-AppxPackage -Path $($desktopAppInstaller.file) -DependencyPath $($vcLibsUwp.file), $($uiLibsUwp.file)
  62.   Remove-Item $tempFolder -recurse -force
  63. }
  64. Write-Host -ForegroundColor Green "--> Updating Winget`n"
  65. Install-Winget
Advertisement
Add Comment
Please, Sign In to add comment