winutil/functions/private/Invoke-WinUtilWingetProgram.ps1

170 lines
5.4 KiB
PowerShell
Raw Normal View History

Function Invoke-WinUtilWingetProgram {
<#
.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(
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383) * Replace Tabs with Spaces to follow the conventions * Add Preprocessing in Compiler * Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from * Code Formatting Changes * Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters * Make Preprocessing more advanced * Move Preprocessing to a separate script file * Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency * Revert commit b5dffd671ff4f870026e4d384f393c0491692ab7 * Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet * Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool * Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments * Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error * Replace Tabs with Spaces to follow the conventions - 'applications.json' File * Code Formatting Changes - 'Copy-Files' Private Function * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool * Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed * Result of 'Invoke-Preprocessing' Script * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00
[Parameter(Mandatory, Position=0)]$Programs,
2024-08-06 15:50:36 -05: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
.PARAMETER scope
Determines the installation mode. Can be "user" or "machine" (For more info look at the winget documentation)
.PARAMETER credential
The PSCredential Object of the user that should be used to run winget
2024-08-06 15:50:36 -05:00
.NOTES
Invoke Winget uses the public variable $Action defined outside the function to determine if a Program should be installed or removed
#>
param (
[string]$wingetId,
[string]$scope = "",
[PScredential]$credential = $null
)
$commonArguments = "--id $wingetId --silent"
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383) * Replace Tabs with Spaces to follow the conventions * Add Preprocessing in Compiler * Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from * Code Formatting Changes * Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters * Make Preprocessing more advanced * Move Preprocessing to a separate script file * Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency * Revert commit b5dffd671ff4f870026e4d384f393c0491692ab7 * Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet * Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool * Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments * Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error * Replace Tabs with Spaces to follow the conventions - 'applications.json' File * Code Formatting Changes - 'Copy-Files' Private Function * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool * Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed * Result of 'Invoke-Preprocessing' Script * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00
$arguments = if ($Action -eq "Install") {
2024-08-06 15:50:36 -05:00
"install $commonArguments --accept-source-agreements --accept-package-agreements $(if ($scope) {" --scope $scope"})"
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383) * Replace Tabs with Spaces to follow the conventions * Add Preprocessing in Compiler * Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from * Code Formatting Changes * Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters * Make Preprocessing more advanced * Move Preprocessing to a separate script file * Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency * Revert commit b5dffd671ff4f870026e4d384f393c0491692ab7 * Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet * Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool * Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments * Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error * Replace Tabs with Spaces to follow the conventions - 'applications.json' File * Code Formatting Changes - 'Copy-Files' Private Function * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool * Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed * Result of 'Invoke-Preprocessing' Script * Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00
} else {
"uninstall $commonArguments"
}
$processParams = @{
FilePath = "winget"
ArgumentList = $arguments
Wait = $true
PassThru = $true
NoNewWindow = $true
}
if ($credential) {
$processParams.credential = $credential
}
2024-08-06 15:50:36 -05:00
return (Start-Process @processParams).ExitCode
}
Function Invoke-Install {
<#
.SYNOPSIS
Contains the Install Logic and return code handling from winget
2024-08-06 15:50:36 -05: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 "Attempt installation of $($Program) with User scope"
$status = Invoke-Winget -wingetId $Program -scope "user"
if ($status -eq 0) {
Write-Host "$($Program) installed successfully with User scope."
return $true
} elseif ($status -eq -1978335189) {
Write-Host "$($Program) No applicable update found"
return $true
}
$userChoice = [System.Windows.MessageBox]::Show("Do you want to attempt $($Program) 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
$status = Invoke-Winget -wingetId $Program -credential $getcreds
if ($status -eq 0) {
Write-Host "$($Program) installed successfully with User prompt."
return $true
}
} else {
Write-Host "Skipping installation with specific user credentials."
}
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
.PARAMETER Program
The Winget ID of the Program that should be uninstalled
#>
param (
[psobject]$Program
)
2024-08-06 15:50:36 -05: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
Write-Host "==========================================="
Write-Host "-- Configuring winget packages ---"
Write-Host "==========================================="
2024-08-06 15:50:36 -05: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
$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"}
}
if (-not $result) {
$failedPackages += $Program
}
}
Set-WinUtilProgressBar -label "$($Action)ation done" -percent 100
return $failedPackages
}