diff --git a/winutil.ps1 b/winutil.ps1 index 9b4881bf..e11e45ff 100644 --- a/winutil.ps1 +++ b/winutil.ps1 @@ -10,7 +10,7 @@ Author : Chris Titus @christitustech Runspace Author: @DeveloperDurp GitHub : https://github.com/ChrisTitusTech - Version : 24.06.04 + Version : 24.06.05 #> param ( [switch]$Debug, @@ -47,7 +47,7 @@ Add-Type -AssemblyName System.Windows.Forms # Variable to sync between runspaces $sync = [Hashtable]::Synchronized(@{}) $sync.PSScriptRoot = $PSScriptRoot -$sync.version = "24.06.04" +$sync.version = "24.06.05" $sync.configs = @{} $sync.ProcessRunning = $false @@ -769,40 +769,79 @@ function Install-WinUtilProgramChoco { #> param( - $ProgramsToInstall, - $manage = "Installing" + [Parameter(Mandatory, Position=0)] + [PsCustomObject]$ProgramsToInstall, + + [Parameter(Position=1)] + [String]$manage = "Installing" ) $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,`nPlease double check your code and re-compile WinUtil." + } + Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 Write-Host "===========================================" - Write-Host "-- insstalling Chocolatey pacakages ---" + Write-Host "-- Configuring Chocolatey pacakages ---" Write-Host "===========================================" Foreach ($Program in $ProgramsToInstall){ Write-Progress -Activity "$manage Applications" -Status "$manage $($Program.choco) $($x + 1) of $count" -PercentComplete $($x/$count*100) if($manage -eq "Installing"){ write-host "Starting install of $($Program.choco) with Chocolatey." try{ - $chocoStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru).ExitCode - if($chocoStatus -eq 0){ + $tryUpgrade = $false + $installOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.install-command.output.txt" + New-Item -ItemType File -Path $installOutputFilePath + $chocoInstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath).ExitCode + if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $installOutputFilePath)) { + $keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"} + if ($keywordsFound) { + $tryUpgrade = $true + } + } + # 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)){ Write-Host "$($Program.choco) installed successfully using Chocolatey." continue } else { - Write-Host "Failed to install $($Program.choco) using Chocolatey." + Write-Host "Failed to install $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $installOutputFilePath)." } - Write-Host "Failed to install $($Program.choco)." } catch { Write-Host "Failed to install $($Program.choco) due to an error: $_" } } - if($manage -eq "Uninstalling"){ - throw "not yet implemented"; - } - $X++ + + if($manage -eq "Uninstalling"){ + write-host "Starting uninstall of $($Program.choco) with Chocolatey." + try{ + $uninstallOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.uninstall-command.output.txt" + New-Item -ItemType File -Path $uninstallOutputFilePath + $chocoUninstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "uninstall $($Program.choco) -y" -Wait -PassThru).ExitCode + if($chocoUninstallStatus -eq 0){ + Write-Host "$($Program.choco) uninstalled successfully using Chocolatey." + continue + } else { + Write-Host "Failed to uninstall $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $uninstallOutputFilePath)." + } + } catch { + Write-Host "Failed to uninstall $($Program.choco) due to an error: $_" + } + } + $x++ } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed + + # Cleanup leftovers files + if(Test-Path -Path $installOutputFilePath){ Remove-Item -Path $installOutputFilePath } + if(Test-Path -Path $installOutputFilePath){ Remove-Item -Path $uninstallOutputFilePath } + return; } Function Install-WinUtilProgramWinget { @@ -823,15 +862,18 @@ Function Install-WinUtilProgramWinget { #> param( - $ProgramsToInstall, - $manage = "Installing" + [Parameter(Mandatory, Position=0)] + [PsCustomObject]$ProgramsToInstall, + + [Parameter(Position=1)] + [String]$manage = "Installing" ) $x = 0 $count = $ProgramsToInstall.Count Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 Write-Host "===========================================" - Write-Host "-- installing winget packages ---" + Write-Host "-- Configuring winget packages ---" Write-Host "===========================================" Foreach ($Program in $ProgramsToInstall){ $failedPackages = @() @@ -882,7 +924,7 @@ Function Install-WinUtilProgramWinget { } } catch { Write-Host "Failed to install $($Program.winget). With winget" - $failedPackages += $($Program.winget) + $failedPackages += $Program } } if($manage -eq "Uninstalling"){ @@ -893,11 +935,11 @@ Function Install-WinUtilProgramWinget { Write-Host "Failed to uninstall $($Program.winget)." } else { Write-Host "$($Program.winget) uninstalled successfully." - $failedPackages += $($Program.winget) + $failedPackages += $Program } } catch { Write-Host "Failed to uninstall $($Program.winget) due to an error: $_" - $failedPackages += $($Program.winget) + $failedPackages += $Program } } $X++ @@ -5147,10 +5189,10 @@ function Invoke-WPFUnInstall { # Install all selected programs in new window if($packagesWinget.Count -gt 0){ - Install-WinUtilProgramWinget -ProgramsToInstall $PackagesToInstall -Manage "Uninstalling" + Install-WinUtilProgramWinget -ProgramsToInstall $packagesWinget -Manage "Uninstalling" } if($packagesChoco.Count -gt 0){ - Install-WinUtilProgramChoco -ProgramsToInstall $PackagesToInstall -Manage "Uninstalling" + Install-WinUtilProgramChoco -ProgramsToInstall $packagesChoco -Manage "Uninstalling" } $ButtonType = [System.Windows.MessageBoxButton]::OK @@ -5166,7 +5208,7 @@ function Invoke-WPFUnInstall { } Catch { Write-Host "===========================================" - Write-Host "-- Winget failed to install ---" + Write-Host "Error: $_" Write-Host "===========================================" } $sync.ProcessRunning = $False