Files
winutil/functions/private/Install-WinUtilProgramWinget.ps1
T

107 lines
5.4 KiB
PowerShell
Raw Normal View History

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