From 2354645b479b9ca7bcf377ea4e199b262f4b6bb4 Mon Sep 17 00:00:00 2001 From: Chris Titus Date: Sat, 20 Apr 2024 21:48:55 -0500 Subject: [PATCH] Fix ALL Program Installs (#1856) * Compile Winutil * Update Invoke-WPFOOSU * Compile Winutil * Modify Winget program installs did a waterfall type installer. First trys systemwide at admin prompt, then user scope, then unelevated with no scope. This should fix ALL program installs. * Compile Winutil --------- Co-authored-by: ChrisTitusTech --- .../private/Install-WinUtilProgramWinget.ps1 | 53 +++++++++++-------- winutil.ps1 | 53 +++++++++++-------- 2 files changed, 60 insertions(+), 46 deletions(-) diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index 393857c..4f84000 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -28,39 +28,46 @@ Function Install-WinUtilProgramWinget { Write-Progress -Activity "$manage Applications" -Status "$manage $Program $($x + 1) of $count" -PercentComplete $($x/$count*100) 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. - # With WinGet, not all installers honor any of the following: System-wide or User Installs OR Silent Install Mode. + # Install package via ID, if it fails try again with different scope and then with an unelevated prompt. + # Since Install-WinGetPackage might not be directly available, we use winget install command as a workaround. + # Winget, not all installers honor any of the following: System-wide, User Installs, or Unelevated Prompt 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." + try { + $status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Attempt with User scope" + $status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Attempt with Unelevated prompt" + $status = $(Start-Process -FilePath "powershell" -ArgumentList "-Command Start-Process winget -ArgumentList 'install --id $Program --silent --accept-source-agreements --accept-package-agreements' -Verb runAsUser" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Failed to install $Program." + } else { + Write-Host "$Program installed successfully with Unelevated prompt." + } } else { - Write-Host "$Program installed successfully." + Write-Host "$Program installed successfully with User scope." } } else { Write-Host "$Program installed successfully." } - } else { - Write-Host "$Program installed successfully." + } catch { + Write-Host "Failed to install $Program due to an error: $_" } } 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 -MatchOption Equals -Source winget).Status) - if ("$status" -ne "Ok") { - Write-Host "Failed to uninstall $Program." - } else { - Write-Host "$Program uninstalled successfully." + # Uninstall package via ID using winget directly. + try { + $status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $Program --silent" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Failed to uninstall $Program." + } else { + Write-Host "$Program uninstalled successfully." + } + } catch { + Write-Host "Failed to uninstall $Program due to an error: $_" } - } + } $X++ } diff --git a/winutil.ps1 b/winutil.ps1 index 82772dd..ad6a4c0 100644 --- a/winutil.ps1 +++ b/winutil.ps1 @@ -727,39 +727,46 @@ Function Install-WinUtilProgramWinget { Write-Progress -Activity "$manage Applications" -Status "$manage $Program $($x + 1) of $count" -PercentComplete $($x/$count*100) 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. - # With WinGet, not all installers honor any of the following: System-wide or User Installs OR Silent Install Mode. + # Install package via ID, if it fails try again with different scope and then with an unelevated prompt. + # Since Install-WinGetPackage might not be directly available, we use winget install command as a workaround. + # Winget, not all installers honor any of the following: System-wide, User Installs, or Unelevated Prompt 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." + try { + $status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Attempt with User scope" + $status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Attempt with Unelevated prompt" + $status = $(Start-Process -FilePath "powershell" -ArgumentList "-Command Start-Process winget -ArgumentList 'install --id $Program --silent --accept-source-agreements --accept-package-agreements' -Verb runAsUser" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Failed to install $Program." + } else { + Write-Host "$Program installed successfully with Unelevated prompt." + } } else { - Write-Host "$Program installed successfully." + Write-Host "$Program installed successfully with User scope." } } else { Write-Host "$Program installed successfully." } - } else { - Write-Host "$Program installed successfully." + } catch { + Write-Host "Failed to install $Program due to an error: $_" } } 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 -MatchOption Equals -Source winget).Status) - if ("$status" -ne "Ok") { - Write-Host "Failed to uninstall $Program." - } else { - Write-Host "$Program uninstalled successfully." + # Uninstall package via ID using winget directly. + try { + $status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $Program --silent" -Wait -PassThru).ExitCode + if($status -ne 0){ + Write-Host "Failed to uninstall $Program." + } else { + Write-Host "$Program uninstalled successfully." + } + } catch { + Write-Host "Failed to uninstall $Program due to an error: $_" } - } + } $X++ }