2024-09-10 20:02:22 +02:00
|
|
|
Function Install-WinUtilProgramWinget {
|
2024-07-31 04:13:30 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Runs the designated action on the provided programs using Winget
|
|
|
|
|
|
|
|
|
|
.PARAMETER Programs
|
|
|
|
|
A list of programs to process
|
|
|
|
|
|
|
|
|
|
.PARAMETER action
|
|
|
|
|
The action to perform on the programs, can be either 'Install' or 'Uninstall'
|
|
|
|
|
|
|
|
|
|
.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-actionr/winget/returnCodes.md
|
|
|
|
|
#>
|
|
|
|
|
|
|
|
|
|
param(
|
2024-08-06 23:35:17 +03:00
|
|
|
[Parameter(Mandatory, Position=0)]$Programs,
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
[Parameter(Mandatory, Position=1)]
|
|
|
|
|
[ValidateSet("Install", "Uninstall")]
|
|
|
|
|
[String]$Action
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Function Invoke-Winget {
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Invokes the winget.exe with the provided arguments and return the exit code
|
|
|
|
|
|
|
|
|
|
.PARAMETER wingetId
|
|
|
|
|
The Id of the Program that Winget should Install/Uninstall
|
|
|
|
|
|
|
|
|
|
.NOTES
|
|
|
|
|
Invoke Winget uses the public variable $Action defined outside the function to determine if a Program should be installed or removed
|
|
|
|
|
#>
|
|
|
|
|
param (
|
2025-11-17 13:10:44 -06:00
|
|
|
[string]$wingetId
|
2024-07-31 04:13:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$commonArguments = "--id $wingetId --silent"
|
2024-08-06 23:35:17 +03:00
|
|
|
$arguments = if ($Action -eq "Install") {
|
2025-11-17 13:10:44 -06:00
|
|
|
"install $commonArguments --accept-source-agreements --accept-package-agreements"
|
2024-08-06 23:35:17 +03:00
|
|
|
} else {
|
2024-07-31 04:13:30 +02:00
|
|
|
"uninstall $commonArguments"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$processParams = @{
|
|
|
|
|
FilePath = "winget"
|
|
|
|
|
ArgumentList = $arguments
|
|
|
|
|
Wait = $true
|
|
|
|
|
PassThru = $true
|
|
|
|
|
NoNewWindow = $true
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 15:50:36 -05:00
|
|
|
return (Start-Process @processParams).ExitCode
|
2024-07-31 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function Invoke-Install {
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Contains the Install Logic and return code handling from winget
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
.PARAMETER Program
|
|
|
|
|
The Winget ID of the Program that should be installed
|
|
|
|
|
#>
|
|
|
|
|
param (
|
|
|
|
|
[string]$Program
|
|
|
|
|
)
|
|
|
|
|
$status = Invoke-Winget -wingetId $Program
|
|
|
|
|
if ($status -eq 0) {
|
|
|
|
|
Write-Host "$($Program) installed successfully."
|
|
|
|
|
return $true
|
|
|
|
|
} elseif ($status -eq -1978335189) {
|
|
|
|
|
Write-Host "$($Program) No applicable update found"
|
|
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Write-Host "Failed to install $($Program)."
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function Invoke-Uninstall {
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Contains the Uninstall Logic and return code handling from winget
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
.PARAMETER Program
|
|
|
|
|
The Winget ID of the Program that should be uninstalled
|
|
|
|
|
#>
|
|
|
|
|
param (
|
|
|
|
|
[psobject]$Program
|
|
|
|
|
)
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
try {
|
|
|
|
|
$status = Invoke-Winget -wingetId $Program
|
|
|
|
|
if ($status -eq 0) {
|
|
|
|
|
Write-Host "$($Program) uninstalled successfully."
|
|
|
|
|
return $true
|
|
|
|
|
} else {
|
|
|
|
|
Write-Host "Failed to uninstall $($Program)."
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
Write-Host "Failed to uninstall $($Program) due to an error: $_"
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$count = $Programs.Count
|
|
|
|
|
$failedPackages = @()
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
Write-Host "==========================================="
|
|
|
|
|
Write-Host "-- Configuring winget packages ---"
|
|
|
|
|
Write-Host "==========================================="
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
for ($i = 0; $i -lt $count; $i++) {
|
|
|
|
|
$Program = $Programs[$i]
|
|
|
|
|
$result = $false
|
|
|
|
|
Set-WinUtilProgressBar -label "$Action $($Program)" -percent ($i / $count * 100)
|
|
|
|
|
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i / $count)})
|
2024-08-06 15:50:36 -05:00
|
|
|
|
2024-07-31 04:13:30 +02:00
|
|
|
$result = switch ($Action) {
|
|
|
|
|
"Install" {Invoke-Install -Program $Program}
|
|
|
|
|
"Uninstall" {Invoke-Uninstall -Program $Program}
|
2024-08-06 15:50:36 -05:00
|
|
|
default {throw "[Install-WinUtilProgramWinget] Invalid action: $Action"}
|
2024-07-31 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (-not $result) {
|
|
|
|
|
$failedPackages += $Program
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Set-WinUtilProgressBar -label "$($Action)ation done" -percent 100
|
|
|
|
|
return $failedPackages
|
|
|
|
|
}
|