Files
winutil/functions/private/Install-WinUtilProgramWinget.ps1
T

76 lines
3.5 KiB
PowerShell
Raw Normal View History

+3
2023-03-07 12:28:00 -08:00
Function Install-WinUtilProgramWinget {
<#
2023-10-19 17:12:55 -05:00
.SYNOPSIS
Manages the provided programs using Winget
.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.
+3
2023-03-07 12:28:00 -08:00
#>
+2
2023-05-09 13:14:27 -05:00
param(
$ProgramsToInstall,
$manage = "Installing"
)
+3
2023-03-07 12:28:00 -08:00
$x = 0
$count = $($ProgramsToInstall -split ",").Count
+2
2023-05-09 13:14:27 -05:00
Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0
+3
2023-03-07 12:28:00 -08:00
Foreach ($Program in $($ProgramsToInstall -split ",")){
2023-10-19 17:12:55 -05:00
+2
2023-05-09 13:14:27 -05:00
Write-Progress -Activity "$manage Applications" -Status "$manage $Program $($x + 1) of $count" -PercentComplete $($x/$count*100)
if($manage -eq "Installing"){
2024-04-20 21:48:55 -05:00
# 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.
+1
2024-04-20 16:38:43 -05:00
# This is up to the individual package maintainers to enable these options. Aka. not as clean as Linux Package Managers.
2024-04-20 21:48:55 -05:00
try {
$status = $(Start-Process -FilePath "winget" -ArgumentList "install --id $Program --silent --accept-source-agreements --accept-package-agreements" -Wait -PassThru).ExitCode
if($status -ne 0){
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
if($status -ne 0){
Write-Host "Attempt with Unelevated prompt"
$status = $(Start-Process -FilePath "powershell" -ArgumentList "-Command Start-Process winget -ArgumentList 'install --id $Program --silent --accept-source-agreements --accept-package-agreements' -Verb runAsUser" -Wait -PassThru).ExitCode
if($status -ne 0){
Write-Host "Failed to install $Program."
} else {
Write-Host "$Program installed successfully with Unelevated prompt."
}
+1
2024-04-20 16:38:43 -05:00
} else {
2024-04-20 21:48:55 -05:00
Write-Host "$Program installed successfully with User scope."
+1
2024-04-20 16:38:43 -05:00
}
} else {
Write-Host "$Program installed successfully."
}
2024-04-20 21:48:55 -05:00
} catch {
Write-Host "Failed to install $Program due to an error: $_"
+1
2024-04-20 16:38:43 -05:00
}
+2
2023-05-09 13:14:27 -05:00
}
if($manage -eq "Uninstalling"){
2024-04-20 21:48:55 -05:00
# Uninstall package via ID using winget directly.
try {
$status = $(Start-Process -FilePath "winget" -ArgumentList "uninstall --id $Program --silent" -Wait -PassThru).ExitCode
if($status -ne 0){
Write-Host "Failed to uninstall $Program."
} else {
Write-Host "$Program uninstalled successfully."
}
} catch {
Write-Host "Failed to uninstall $Program due to an error: $_"
+1
2024-04-20 16:38:43 -05:00
}
2024-04-20 21:48:55 -05:00
}
+3
2023-03-07 12:28:00 -08:00
$X++
}
+2
2023-05-09 13:14:27 -05:00
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed
+8
2024-03-21 16:23:24 -07:00
}