2024-06-04 20:27:27 -07:00
function Install-WinUtilProgramChoco {
<#
. SYNOPSIS
Manages the provided programs using Chocolatey
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
.PARAMETER ProgramsToInstall
A list of programs to manage
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
.PARAMETER manage
The action to perform on the programs, can be either 'Installing' or 'Uninstalling'
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
. NOTES
The triple quotes are required any time you need a " in a normal script block.
#>
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
param (
[ Parameter ( Mandatory , Position = 0 )]
[ PsCustomObject ] $ProgramsToInstall ,
[ Parameter ( Position = 1 )]
[ String ] $manage = "Installing"
)
2024-06-29 01:15:39 +03:00
2024-06-04 20:27:27 -07:00
$x = 0
$count = $ProgramsToInstall . Count
# This check isn't really necessary, as there's a couple of checks before this Private Function gets called, but just to make sure ;)
if ( $count -le 0 ) {
throw "Private Function 'Install-WinUtilProgramChoco' expected Parameter 'ProgramsToInstall' to be of size 1 or greater, instead got $count , `n Please double check your code and re-compile WinUtil."
}
Write-Progress -Activity " $manage Applications" -Status "Starting" -PercentComplete 0
Write-Host "==========================================="
Write-Host "-- Configuring Chocolatey pacakages ---"
Write-Host "==========================================="
2024-08-06 23:35:17 +03:00
Foreach ( $Program in $ProgramsToInstall ) {
2024-06-04 20:27:27 -07:00
Write-Progress -Activity " $manage Applications" -Status " $manage $( $Program . choco ) $( $x + 1 ) of $count " -PercentComplete $ ( $x / $count * 100 )
2024-08-06 23:35:17 +03:00
if ( $manage -eq "Installing" ) {
2024-06-04 20:27:27 -07:00
write-host "Starting install of $( $Program . choco ) with Chocolatey."
2024-08-06 23:35:17 +03:00
try {
2024-06-04 20:27:27 -07:00
$tryUpgrade = $false
2024-08-06 23:35:17 +03:00
$installOutputFilePath = " $env:TEMP \Install-WinUtilProgramChoco.install-command.output.txt"
2024-06-04 20:27:27 -07:00
New-Item -ItemType File -Path $installOutputFilePath
2024-08-06 23:35:17 +03:00
$chocoInstallStatus = $ ( Start-Process -FilePath "choco" -ArgumentList "install $( $Program . choco ) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath ). ExitCode
2024-06-04 20:27:27 -07:00
if (( $chocoInstallStatus -eq 0 ) -AND ( Test-Path -Path $installOutputFilePath )) {
$keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object { $_ -match "reinstall" -OR $_ -match "already installed" }
2024-08-06 23:35:17 +03:00
if ( $keywordsFound ) {
$tryUpgrade = $true
}
2024-06-04 20:27:27 -07:00
}
2024-08-06 23:35:17 +03:00
# TODO: Implement the Upgrade part using 'choco upgrade' command, this will make choco consistent with WinGet, as WinGet tries to Upgrade when you use the install command.
if ( $tryUpgrade ) {
throw "Automatic Upgrade for Choco isn't implemented yet, a feature to make it consistent with WinGet, the install command using choco simply failed because $( $Program . choco ) is already installed."
}
if (( $chocoInstallStatus -eq 0 ) -AND ( $tryUpgrade -eq $false )) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . choco ) installed successfully using Chocolatey."
2024-07-25 23:19:45 +02:00
$X ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Normal" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
continue
} else {
Write-Host "Failed to install $( $Program . choco ) using Chocolatey, Chocolatey output: `n`n $( Get-Content -Path $installOutputFilePath ) ."
2024-07-25 23:19:45 +02:00
$X ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Error" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
}
} catch {
Write-Host "Failed to install $( $Program . choco ) due to an error: $_ "
2024-07-25 23:19:45 +02:00
$X ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Error" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
}
}
2024-08-06 23:35:17 +03:00
if ( $manage -eq "Uninstalling" ) {
2024-06-04 20:27:27 -07:00
write-host "Starting uninstall of $( $Program . choco ) with Chocolatey."
2024-08-06 23:35:17 +03:00
try {
$uninstallOutputFilePath = " $env:TEMP \Install-WinUtilProgramChoco.uninstall-command.output.txt"
2024-06-04 20:27:27 -07:00
New-Item -ItemType File -Path $uninstallOutputFilePath
2024-08-06 23:35:17 +03:00
$chocoUninstallStatus = $ ( Start-Process -FilePath "choco" -ArgumentList "uninstall $( $Program . choco ) -y" -Wait -PassThru ). ExitCode
if ( $chocoUninstallStatus -eq 0 ) {
2024-06-04 20:27:27 -07:00
Write-Host " $( $Program . choco ) uninstalled successfully using Chocolatey."
2024-07-25 23:19:45 +02:00
$x ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Normal" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
continue
} else {
Write-Host "Failed to uninstall $( $Program . choco ) using Chocolatey, Chocolatey output: `n`n $( Get-Content -Path $uninstallOutputFilePath ) ."
2024-07-25 23:19:45 +02:00
$x ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Error" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
}
} catch {
Write-Host "Failed to uninstall $( $Program . choco ) due to an error: $_ "
2024-07-25 23:19:45 +02:00
$x ++
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Error" -value ( $x / $count ) })
2024-06-04 20:27:27 -07:00
}
2024-08-06 23:35:17 +03:00
}
2024-06-04 20:27:27 -07:00
}
Write-Progress -Activity " $manage Applications" -Status "Finished" -Completed
# Cleanup leftovers files
2024-08-06 23:35:17 +03:00
if ( Test-Path -Path $installOutputFilePath ) { Remove-Item -Path $installOutputFilePath }
if ( Test-Path -Path $uninstallOutputFilePath ) { Remove-Item -Path $uninstallOutputFilePath }
2024-06-04 20:27:27 -07:00
return ;
}