wingetautoupdate/Winget-AutoUpdate/functions/Update-WinGet.ps1

55 lines
2.4 KiB
PowerShell
Raw Normal View History

2023-10-12 02:22:15 +00:00
#Function to download and update WinGet
2023-10-11 20:19:34 +00:00
Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) {
2023-10-12 17:42:01 +00:00
$download_string = "-> Downloading WinGet MSIXBundle for App Installer..."
$install_string = "-> Installing WinGet MSIXBundle for App Installer..."
$success_string = "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully"
$reset_string = "-> WinGet sources reset."
$fail_string = "-> Failed to intall WinGet MSIXBundle for App Installer..."
2023-10-11 20:19:34 +00:00
#Download WinGet MSIXBundle
2023-10-12 02:22:15 +00:00
switch ($Log) {
2023-10-12 17:42:01 +00:00
$true {Write-ToLog $download_string}
Default {Write-Host $download_string}
2023-10-12 02:22:15 +00:00
}
2023-10-11 20:19:34 +00:00
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")
#Install WinGet MSIXBundle in SYSTEM context
try {
2023-10-12 02:22:15 +00:00
switch ($Log) {
2023-10-12 17:42:01 +00:00
$true {Write-ToLog $install_string}
Default {Write-Host $install_string}
2023-10-12 02:22:15 +00:00
}
2023-10-11 20:19:34 +00:00
Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
2023-10-12 02:22:15 +00:00
switch ($Log) {
2023-10-12 17:42:01 +00:00
$true {Write-ToLog $success_string "green"}
Default {Write-host $success_string -ForegroundColor Green}
2023-10-12 02:22:15 +00:00
}
2023-10-11 20:19:34 +00:00
#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
2023-10-12 02:22:15 +00:00
switch ($Log) {
2023-10-12 17:42:01 +00:00
$true {Write-ToLog $reset_string "green"}
Default {Write-Host $reset_string -ForegroundColor Green}
2023-10-12 02:22:15 +00:00
}
2023-10-11 20:19:34 +00:00
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
}
}
catch {
2023-10-12 02:22:15 +00:00
switch ($Log) {
2023-10-12 17:42:01 +00:00
$true {Write-ToLog $fail_string "red"}
Default {Write-Host $fail_string -ForegroundColor Red}
2023-10-12 02:22:15 +00:00
}
Update-StoreApps
2023-10-11 20:19:34 +00:00
}
#Remove WinGet MSIXBundle
Remove-Item -Path "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue
}