Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winget install return code handling & Window management #1992

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions functions/private/Install-WinUtilProgramWinget.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Function Install-WinUtilProgramWinget {

.NOTES
The triple quotes are required any time you need a " in a normal script block.
The winget Return codes are documented here: https://github.com/microsoft/winget-cli/blob/master/doc/windows/package-manager/winget/returnCodes.md
#>

param(
Expand All @@ -35,22 +36,30 @@ Function Install-WinUtilProgramWinget {
# This is up to the individual package maintainers to enable these options. Aka. not as clean as Linux Package Managers.
Write-Host "Starting install of $($Program.winget) with winget."
try {
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru -NoNewWindow).ExitCode
if($status -eq 0){
Write-Host "$($Program.winget) installed successfully."
continue
}
if ($status -eq -1978335189){
Write-Host "$($Program.winget) No applicable update found"
continue
}
Write-Host "Attempt with User scope"
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru -NoNewWindow).ExitCode
if($status -eq 0){
Write-Host "$($Program.winget) installed successfully with User scope."
continue
}
if ($status -eq -1978335189){
Write-Host "$($Program.winget) No applicable update found"
continue
}
Write-Host "Attempt with User prompt"
$userChoice = [System.Windows.MessageBox]::Show("Do you want to attempt $($Program.winget) installation with specific user credentials? Select 'Yes' to proceed or 'No' to skip.", "User Credential Prompt", [System.Windows.MessageBoxButton]::YesNo)
if ($userChoice -eq 'Yes') {
$getcreds = Get-Credential
$process = Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Credential $getcreds -PassThru
$process = Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Credential $getcreds -PassThru -NoNewWindow
Wait-Process -Id $process.Id
$status = $process.ExitCode
} else {
Expand All @@ -60,6 +69,10 @@ Function Install-WinUtilProgramWinget {
Write-Host "$($Program.winget) installed successfully with User prompt."
continue
}
if ($status -eq -1978335189){
Write-Host "$($Program.winget) No applicable update found"
continue
}
} catch {
Write-Host "Failed to install $($Program.winget). With winget"
$failedPackages += $($Program.winget)
Expand All @@ -68,7 +81,7 @@ Function Install-WinUtilProgramWinget {
if($manage -eq "Uninstalling"){
# Uninstall package via ID using winget directly.
try {
$status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $($Program.winget) --silent" -Wait -PassThru).ExitCode
$status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $($Program.winget) --silent" -Wait -PassThru -NoNewWindow).ExitCode
if($status -ne 0){
Write-Host "Failed to uninstall $($Program.winget)."
} else {
Expand Down