From 5bbba3e44f60ec007cb2ec6e84696cad81cfab5c Mon Sep 17 00:00:00 2001 From: Rux Date: Thu, 18 Apr 2024 08:19:32 -0700 Subject: [PATCH] Update Winget Install and Uninstall / Add NuGet and Microsoft.Winget.Client Modules (#1830) * - Changed how WinGet installs and uninstalls are handled, by utilizing and . * Update Winget Install and Uninstall / Add NuGet and Microsoft.Winget.Client Modules - Fixed commands for installing and uninstalling programs through WinGet. - Added NuGet Package Providers (thanks @Marterich) - Added Microsoft.WinGet.Client Module (thanks @Marterich) --- .../private/Install-WinUtilProgramWinget.ps1 | 41 ++++++++++++++----- functions/private/Install-WinUtilWinget.ps1 | 3 ++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index 7bfedd9..393857c 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -1,7 +1,6 @@ Function Install-WinUtilProgramWinget { <# - .SYNOPSIS Manages the provided programs using Winget @@ -13,7 +12,6 @@ Function Install-WinUtilProgramWinget { .NOTES The triple quotes are required any time you need a " in a normal script block. - #> param( @@ -30,18 +28,41 @@ Function Install-WinUtilProgramWinget { Write-Progress -Activity "$manage Applications" -Status "$manage $Program $($x + 1) of $count" -PercentComplete $($x/$count*100) if($manage -eq "Installing"){ - # --scope=machine when installing non-UWP apps with winget fails with error code 0x80070005. - # Removed argument while testing new Winget install method. - # Open issue on winget-cli github repo: https://github.com/microsoft/winget-cli/issues/3936 - Start-Process -FilePath winget -ArgumentList "install -e --accept-source-agreements --accept-package-agreements --silent $Program" -NoNewWindow -Wait + # Install package via ID, if it fails try again with different scope. + # Install-WinGetPackage always returns "InstallerErrorCode" 0, so we have to check the "Status" of the install. + # With WinGet, not all installers honor any of the following: System-wide or User Installs OR Silent Install Mode. + # This is up to the individual package maintainers to enable these options. Aka. not as clean as Linux Package Managers. + $status=$((Install-WinGetPackage -Id $Program -Scope SystemOrUnknown -Mode Silent -Source winget -MatchOption Equals).Status) + if($status -ne "Ok"){ + Write-Host "Not System" + $status=$((Install-WinGetPackage -Id $Program -Scope UserOrUnknown -Mode Silent -Source winget -MatchOption Equals).Status) + if($status -ne "Ok"){ + Write-Host "Not User" + $status=$((Install-WinGetPackage -Id $Program -Scope Any -Mode Silent -Source winget -MatchOption Equals).Status) + if($status -ne "Ok"){ + Write-Host "Failed to install $Program." + } else { + Write-Host "$Program installed successfully." + } + } else { + Write-Host "$Program installed successfully." + } + } else { + Write-Host "$Program installed successfully." + } } if($manage -eq "Uninstalling"){ - Start-Process -FilePath winget -ArgumentList "uninstall -e --accept-source-agreements --purge --force --silent $Program" -NoNewWindow -Wait - } - + # Uninstall package via ID. + # Uninstall-WinGetPackage always returns "InstallerErrorCode" 0, so we have to check the "Status" of the uninstall. + $status=$((Uninstall-WinGetPackage -Id $Program -Mode Silent -MatchOption Equals -Source winget).Status) + if ("$status" -ne "Ok") { + Write-Host "Failed to uninstall $Program." + } else { + Write-Host "$Program uninstalled successfully." + } + } $X++ } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed - } diff --git a/functions/private/Install-WinUtilWinget.ps1 b/functions/private/Install-WinUtilWinget.ps1 index 2bdba08..093a5c3 100644 --- a/functions/private/Install-WinUtilWinget.ps1 +++ b/functions/private/Install-WinUtilWinget.ps1 @@ -43,6 +43,9 @@ function Install-WinUtilWinget { Write-Host "Manually adding Winget Sources, from Winget CDN." Add-AppxPackage -Path https://cdn.winget.microsoft.com/cache/source.msix #Seems some installs of Winget don't add the repo source, this should makes sure that it's installed every time. Write-Host "Winget Installed" -ForegroundColor Green + Write-Host "Enabling NuGet and Module..." + Install-PackageProvider -Name NuGet -Force + Install-Module -Name Microsoft.WinGet.Client -Force # Winget only needs a refresh of the environment variables to be used. Write-Output "Refreshing Environment Variables...`n" $ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")