2023-03-07 12:28:00 -08:00
Function Install-WinUtilProgramWinget {
2024-06-29 01:15:39 +03:00
2023-03-07 12:28:00 -08:00
<#
2023-10-19 17:12:55 -05:00
. SYNOPSIS
2024-06-04 20:27:27 -07:00
Manages the provided programs using Winget
2024-06-29 01:15:39 +03:00
2023-10-19 17:12:55 -05:00
.PARAMETER ProgramsToInstall
2024-06-04 20:27:27 -07:00
A list of programs to manage
2024-06-29 01:15:39 +03:00
2023-10-19 17:12:55 -05:00
.PARAMETER manage
2024-06-04 20:27:27 -07:00
The action to perform on the programs, can be either 'Installing' or 'Uninstalling'
2024-06-29 01:15:39 +03:00
2023-10-19 17:12:55 -05:00
. NOTES
2024-06-04 20:27:27 -07:00
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
2023-03-07 12:28:00 -08:00
#>
2024-06-29 01:15:39 +03:00
2023-05-09 13:14:27 -05:00
param (
2024-06-04 20:27:27 -07:00
[ Parameter ( Mandatory , Position = 0 )]
[ PsCustomObject ] $ProgramsToInstall ,
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
[ Parameter ( Position = 1 )]
[ String ] $manage = "Installing"
2023-05-09 13:14:27 -05:00
)
2024-07-08 23:28:22 +03:00
2024-06-04 20:27:27 -07:00
$count = $ProgramsToInstall . Count
2024-06-29 01:15:39 +03:00
2023-05-09 13:14:27 -05:00
Write-Progress -Activity " $manage Applications" -Status "Starting" -PercentComplete 0
2024-06-04 20:27:27 -07:00
Write-Host "==========================================="
Write-Host "-- Configuring winget packages ---"
Write-Host "==========================================="
2024-07-08 23:28:22 +03:00
for ( $i = 0 ; $i -le $ProgramsToInstall . Count ; $i ++) {
$Program = $ProgramsToInstall [ $i ]
2024-06-04 20:27:27 -07:00
$failedPackages = @ ()
2024-07-08 23:28:22 +03:00
Write-Progress -Activity " $manage Applications" -Status " $manage $( $Program . winget ) $( $i + 1 ) of $count " -PercentComplete $ (( $i / $count ) * 100 )
if ( $manage -eq "Installing" ) {
2024-06-29 01:15:39 +03:00
# Install package via ID, if it fails try again with different scope and then with an unelevated prompt.
2024-04-20 21:48:55 -05:00
# 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.
2024-04-20 16:38:43 -05:00
# This is up to the individual package maintainers to enable these options. Aka. not as clean as Linux Package Managers.
2024-06-04 20:27:27 -07:00
Write-Host "Starting install of $( $Program . winget ) with winget."
2024-04-20 21:48:55 -05:00
try {
2024-06-04 20:27:27 -07:00
$status = $ ( Start-Process -FilePath "winget" -ArgumentList "install --id $( $Program . winget ) --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru -NoNewWindow ). ExitCode
2024-07-08 23:28:22 +03:00
if ( $status -eq 0 ) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . winget ) installed successfully."
continue
}
2024-07-08 23:28:22 +03:00
if ( $status -eq -1978335189 ) {
2024-06-04 20:27:27 -07:00
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 -NoNewWindow ). ExitCode
2024-07-08 23:28:22 +03:00
if ( $status -eq 0 ) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . winget ) installed successfully with User scope."
continue
}
2024-07-08 23:28:22 +03:00
if ( $status -eq -1978335189 ) {
2024-06-04 20:27:27 -07:00
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 -NoNewWindow
Wait-Process -Id $process . Id
$status = $process . ExitCode
2024-04-20 16:38:43 -05:00
} else {
2024-06-04 20:27:27 -07:00
Write-Host "Skipping installation with specific user credentials."
}
2024-07-08 23:28:22 +03:00
if ( $status -eq 0 ) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . winget ) installed successfully with User prompt."
continue
}
2024-07-08 23:28:22 +03:00
if ( $status -eq -1978335189 ) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . winget ) No applicable update found"
continue
2024-04-20 16:38:43 -05:00
}
2024-04-20 21:48:55 -05:00
} catch {
2024-06-04 20:27:27 -07:00
Write-Host "Failed to install $( $Program . winget ) . With winget"
$failedPackages += $Program
2024-04-20 16:38:43 -05:00
}
2023-05-09 13:14:27 -05:00
}
2024-07-08 23:28:22 +03:00
elseif ( $manage -eq "Uninstalling" ) {
2024-04-20 21:48:55 -05:00
# Uninstall package via ID using winget directly.
try {
2024-06-04 20:27:27 -07:00
$status = $ ( Start-Process -FilePath "winget" -ArgumentList "uninstall --id $( $Program . winget ) --silent" -Wait -PassThru -NoNewWindow ). ExitCode
2024-07-08 23:28:22 +03:00
if ( $status -ne 0 ) {
2024-06-04 20:27:27 -07:00
Write-Host "Failed to uninstall $( $Program . winget ) ."
2024-04-20 21:48:55 -05:00
} else {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . winget ) uninstalled successfully."
$failedPackages += $Program
2024-04-20 21:48:55 -05:00
}
} catch {
2024-06-04 20:27:27 -07:00
Write-Host "Failed to uninstall $( $Program . winget ) due to an error: $_ "
$failedPackages += $Program
2024-04-20 16:38:43 -05:00
}
2024-04-20 21:48:55 -05:00
}
2024-07-08 23:28:22 +03:00
else {
throw "[Install-WinUtilProgramWinget] Invalid Value for Parameter 'manage', Provided Value is: $manage "
}
2023-03-07 12:28:00 -08:00
}
2023-05-09 13:14:27 -05:00
Write-Progress -Activity " $manage Applications" -Status "Finished" -Completed
2024-06-04 20:27:27 -07:00
return $failedPackages ;
2024-03-21 16:23:24 -07:00
}