forked from ChrisTitusTech/winutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'test-2024-04-20-2-pr2.5' into test-2024-04-20-2-merge1
- Loading branch information
Showing
4 changed files
with
123 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function Install-WinUtilProgramChoco { | ||
<# | ||
.SYNOPSIS | ||
Manages the provided programs using Chocolatey | ||
.PARAMETER ProgramsToInstall | ||
A list of programs to manage | ||
.PARAMETER manage | ||
The action to perform on the programs, can be either 'Installing' or 'Uninstalling' | ||
.NOTES | ||
The triple quotes are required any time you need a " in a normal script block. | ||
#> | ||
|
||
param( | ||
$ProgramsToInstall, | ||
$manage = "Installing" | ||
) | ||
|
||
$x = 0 | ||
$count = $ProgramsToInstall.Count | ||
|
||
Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 | ||
Write-Host "===========================================" | ||
Write-Host "-- insstalling 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){ | ||
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)." | ||
} catch { | ||
Write-Host "Failed to install $($Program.choco) due to an error: $_" | ||
} | ||
} | ||
if($manage -eq "Uninstalling"){ | ||
throw "not yet implemented"; | ||
} | ||
$X++ | ||
} | ||
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,88 @@ | ||
Function Install-WinUtilProgramWinget { | ||
|
||
<# | ||
.SYNOPSIS | ||
Manages the provided programs using Winget | ||
Manages the provided programs using Winget | ||
.PARAMETER ProgramsToInstall | ||
A list of programs to manage | ||
A list of programs to manage | ||
.PARAMETER manage | ||
The action to perform on the programs, can be either 'Installing' or 'Uninstalling' | ||
The action to perform on the programs, can be either 'Installing' or 'Uninstalling' | ||
.NOTES | ||
The triple quotes are required any time you need a " in a normal script block. | ||
The triple quotes are required any time you need a " in a normal script block. | ||
#> | ||
|
||
param( | ||
$ProgramsToInstall, | ||
$manage = "Installing" | ||
$ProgramsToInstall, | ||
$manage = "Installing" | ||
) | ||
|
||
$x = 0 | ||
$count = $($ProgramsToInstall -split ",").Count | ||
|
||
$count = $ProgramsToInstall.Count | ||
Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 | ||
|
||
Foreach ($Program in $($ProgramsToInstall -split ",")){ | ||
|
||
Write-Progress -Activity "$manage Applications" -Status "$manage $Program $($x + 1) of $count" -PercentComplete $($x/$count*100) | ||
Write-Host "===========================================" | ||
Write-Host "-- installing winget packages ---" | ||
Write-Host "===========================================" | ||
Foreach ($Program in $ProgramsToInstall){ | ||
$failedPackages = @() | ||
Write-Progress -Activity "$manage Applications" -Status "$manage $($Program.winget) $($x + 1) of $count" -PercentComplete $($x/$count*100) | ||
if($manage -eq "Installing"){ | ||
# Install package via ID, if it fails try again with different scope and then with an unelevated prompt. | ||
# 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. | ||
# This is up to the individual package maintainers to enable these options. Aka. not as clean as Linux Package Managers. | ||
Write-Host "Starting install of $($Program.winget) with winget." | ||
try { | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode | ||
if($status -eq 0){ | ||
Write-Host "$Program installed successfully." | ||
Write-Host "$($Program.winget) installed successfully." | ||
continue | ||
} | ||
Write-Host "Attempt with User scope" | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --scope user --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode | ||
if($status -eq 0){ | ||
Write-Host "$Program installed successfully with User scope." | ||
Write-Host "$($Program.winget) installed successfully with User scope." | ||
continue | ||
} | ||
Write-Host "Attempt with User prompt" | ||
$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) | ||
$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 --silent --accept-source-agreements --accept-package-agreements" -Credential $getcreds -PassThru | ||
$process = Start-Process -FilePath "winget" -ArgumentList "install --id $($Program.winget) --silent --accept-source-agreements --accept-package-agreements" -Credential $getcreds -PassThru | ||
Wait-Process -Id $process.Id | ||
$status = $process.ExitCode | ||
} else { | ||
Write-Host "Skipping installation with specific user credentials." | ||
} | ||
if($status -eq 0){ | ||
Write-Host "$Program installed successfully with User prompt." | ||
Write-Host "$($Program.winget) installed successfully with User prompt." | ||
continue | ||
} | ||
Write-Host "Attempting installation with Chocolatey as a fallback method" | ||
Install-WinUtilChoco | ||
$status = $(Start-Process -FilePath "choco" -ArgumentList "install $Program -y" -Wait -PassThru).ExitCode | ||
if($status -eq 0){ | ||
Write-Host "$Program installed successfully using Chocolatey." | ||
continue | ||
} | ||
Write-Host "Failed to install $Program. You need to install it manually... Sorry!" | ||
} catch { | ||
Write-Host "Failed to install $Program due to an error: $_" | ||
} | ||
} catch { | ||
Write-Host "Failed to install $($Program.winget). With winget" | ||
$failedPackages += $($Program.winget) | ||
} | ||
} | ||
if($manage -eq "Uninstalling"){ | ||
# Uninstall package via ID using winget directly. | ||
try { | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $Program --silent" -Wait -PassThru).ExitCode | ||
$status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $($Program.winget) --silent" -Wait -PassThru).ExitCode | ||
if($status -ne 0){ | ||
Write-Host "Failed to uninstall $Program." | ||
Write-Host "Failed to uninstall $($Program.winget)." | ||
} else { | ||
Write-Host "$Program uninstalled successfully." | ||
Write-Host "$($Program.winget) uninstalled successfully." | ||
$failedPackages += $($Program.winget) | ||
} | ||
} catch { | ||
Write-Host "Failed to uninstall $Program due to an error: $_" | ||
Write-Host "Failed to uninstall $($Program.winget) due to an error: $_" | ||
$failedPackages += $($Program.winget) | ||
} | ||
} | ||
$X++ | ||
} | ||
|
||
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed | ||
return $failedPackages; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters