Update InstallApps.ps1

This commit is contained in:
Ryan 2024-09-07 19:08:42 +00:00
parent 0a187b753f
commit 08fd25d0d8

View File

@ -1,90 +1,106 @@
# Get the current directory where the script is located
$currentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Function to display a menu and get user input # Function to display a menu and get user input
function Show-Menu { function Show-Menu {
param ( param (
[string]$prompt, [string]$prompt, # The prompt message to display
[array]$options [array]$options # The list of options to present to the user
) )
# Display the prompt and options
Write-Host $prompt Write-Host $prompt
for ($i = 0; $i -lt $options.Length; $i++) { for ($i = 0; $i -lt $options.Length; $i++) {
Write-Host "$($i + 1). $($options[$i])" Write-Host "$($i + 1). $($options[$i])" # Display each option with a number
} }
# Get the user's choice
$choice = Read-Host "Select an option (1-$($options.Length))" $choice = Read-Host "Select an option (1-$($options.Length))"
return $choice return $choice
} }
# Function to select multiple options using spacebar # Function to allow multiple selections using the spacebar
function Select-MultipleOptions { function Select-MultipleOptions {
param ( param (
[string]$prompt, [string]$prompt, # The prompt message to display
[array]$options [array]$options # The list of options to present to the user for multiple selection
) )
Write-Host $prompt Write-Host $prompt
$selectedOptions = @() $selectedOptions = @() # Array to hold the selected options
# Display options with selection state # Display options with selection state (checked/unchecked)
for ($i = 0; $i -lt $options.Length; $i++) { for ($i = 0; $i -lt $options.Length; $i++) {
Write-Host "$($i + 1). $($options[$i]) [ ]" Write-Host "$($i + 1). $($options[$i]) [ ]" # Initial unchecked state for each option
} }
Write-Host "Use spacebar to select/unselect and Enter to confirm." Write-Host "Use spacebar to select/unselect and Enter to confirm."
# Capture user input # Capture user input for multiple selection
while ($true) { while ($true) {
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") # Read key input from the user
if ($key.Character -match '\d') { if ($key.Character -match '\d') { # Check if the input is a number
$index = [int]$key.Character - 1 $index = [int]$key.Character - 1 # Convert character to index
if ($index -ge 0 -and $index -lt $options.Length) { if ($index -ge 0 -and $index -lt $options.Length) { # Validate index range
# Toggle selection state for the chosen option
if ($selectedOptions.Contains($index)) { if ($selectedOptions.Contains($index)) {
$selectedOptions.Remove($index) $selectedOptions.Remove($index) # Remove from selected options if already selected
} else { } else {
$selectedOptions.Add($index) $selectedOptions.Add($index) # Add to selected options if not already selected
} }
} }
} elseif ($key.VirtualKeyCode -eq 13) { } elseif ($key.VirtualKeyCode -eq 13) { # Enter key to confirm selection
break break
} }
} }
# Return the list of selected options
return $selectedOptions | ForEach-Object { $options[$_] } return $selectedOptions | ForEach-Object { $options[$_] }
} }
# Main Script Logic # Main script logic starts here
$mainMenuChoice = Show-Menu "Do you want to:" @("Update all apps", "Install apps") $mainMenuChoice = Show-Menu "Do you want to:" @("Update all apps", "Install apps")
# Handle the main menu choices
switch ($mainMenuChoice) { switch ($mainMenuChoice) {
1 { 1 {
# Update all apps # Option 1: Update all apps
Write-Host "Updating all apps..." Write-Host "Updating all apps..."
Start-Process winget -ArgumentList "upgrade --all" -NoNewWindow -Wait Start-Process winget -ArgumentList "upgrade --all" -NoNewWindow -Wait # Run winget upgrade command
} }
2 { 2 {
# Install apps options # Option 2: Install apps
$installChoice = Show-Menu "Choose an installation option:" @("Fresh install of ALL apps", "Select package groups", "Install select packages only") $installChoice = Show-Menu "Choose an installation option:" @("Fresh install of ALL apps", "Select package groups", "Install select packages only")
# Handle the sub-menu choices for app installation
switch ($installChoice) { switch ($installChoice) {
1 { 1 {
# Fresh install of ALL apps # Sub-option 2a: Fresh install of ALL apps
Write-Host "Performing fresh install of all apps..." Write-Host "Performing fresh install of all apps..."
Get-ChildItem -Path "C:\winget\install\groups\*.ps1" | ForEach-Object { & $_.FullName } # Execute all scripts in the groups folder
Get-ChildItem -Path "$currentDir\install\groups\*.ps1" | ForEach-Object { & $_.FullName }
} }
2 { 2 {
# Select package groups # Sub-option 2b: Select package groups for installation
$packageGroups = Get-ChildItem -Path "C:\winget\install\groups\*.ps1" | Select-Object -ExpandProperty Name # Get the list of group scripts available in the groups folder
$packageGroups = Get-ChildItem -Path "$currentDir\install\groups\*.ps1" | Select-Object -ExpandProperty Name
# Allow the user to select multiple groups
$selectedGroups = Select-MultipleOptions "Select package groups to install:" $packageGroups $selectedGroups = Select-MultipleOptions "Select package groups to install:" $packageGroups
# Run the selected group scripts
foreach ($group in $selectedGroups) { foreach ($group in $selectedGroups) {
Write-Host "Installing group: $group" Write-Host "Installing group: $group"
& "C:\winget\install\groups\$group" & "$currentDir\install\groups\$group"
} }
} }
3 { 3 {
# Install select packages only # Sub-option 2c: Install select packages only
$packages = Get-Content -Path "C:\winget\install\individual\list.ps1" # Read the list of individual packages from the file
$packages = Get-Content -Path "$currentDir\install\individual\list.ps1"
# Allow the user to select multiple packages
$selectedPackages = Select-MultipleOptions "Select individual packages to install:" $packages $selectedPackages = Select-MultipleOptions "Select individual packages to install:" $packages
# Run the selected package installation scripts
foreach ($package in $selectedPackages) { foreach ($package in $selectedPackages) {
Write-Host "Installing package: $package" Write-Host "Installing package: $package"
& $package & $package
@ -93,6 +109,7 @@ switch ($mainMenuChoice) {
} }
} }
default { default {
# Handle invalid choice
Write-Host "Invalid choice, please try again." Write-Host "Invalid choice, please try again."
} }
} }