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)
This commit is contained in:
Jonathan Rux 2024-04-17 21:30:47 -07:00
parent 16eb61b431
commit afeaf9d0bb
2 changed files with 12 additions and 5 deletions

View File

@ -30,11 +30,15 @@ Function Install-WinUtilProgramWinget {
if($manage -eq "Installing"){
# 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.
$status=$((Install-WinGetPackage -Id $Program -Scope SystemOrUnknown -Mode Silent -AcceptPackageAgreement -AcceptSourceAgreement).Status)
# 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"){
$status=$((Install-WinGetPackage -Id $Program -Scope UserOrUnknown -Mode Silent -AcceptPackageAgreement -AcceptSourceAgreement).Status)
Write-Host "Not System"
$status=$((Install-WinGetPackage -Id $Program -Scope UserOrUnknown -Mode Silent -Source winget -MatchOption Equals).Status)
if($status -ne "Ok"){
$status=$((Install-WinGetPackage -Id $Program -Scope Any -Mode Silent -AcceptPackageAgreement -AcceptSourceAgreement).Status)
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 {
@ -50,7 +54,7 @@ Function Install-WinUtilProgramWinget {
if($manage -eq "Uninstalling"){
# 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 -AcceptSourceAgreement).Status)
$status=$((Uninstall-WinGetPackage -Id $Program -Mode Silent -MatchOption Equals -Source winget).Status)
if ("$status" -ne "Ok") {
Write-Host "Failed to uninstall $Program."
} else {
@ -61,4 +65,4 @@ Function Install-WinUtilProgramWinget {
}
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed
}
}

View File

@ -41,6 +41,9 @@ function Install-WinUtilWinget {
Write-Host "Installing Winget w/ Prerequsites`r"
Add-AppxProvisionedPackage -Online -PackagePath $ENV:TEMP\Microsoft.DesktopAppInstaller.msixbundle -DependencyPackagePath $ENV:TEMP\Microsoft.VCLibs.x64.Desktop.appx, $ENV:TEMP\Microsoft.UI.Xaml.x64.appx -LicensePath $ENV:TEMP\License1.xml
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")