From 08fd25d0d8860b5815d1c761874274fcb646c947 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 7 Sep 2024 19:08:42 +0000 Subject: [PATCH] Update InstallApps.ps1 --- InstallApps.ps1 | 75 ++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/InstallApps.ps1 b/InstallApps.ps1 index 0908afa..3009cfd 100644 --- a/InstallApps.ps1 +++ b/InstallApps.ps1 @@ -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 Show-Menu { param ( - [string]$prompt, - [array]$options + [string]$prompt, # The prompt message to display + [array]$options # The list of options to present to the user ) - + + # Display the prompt and options Write-Host $prompt 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))" return $choice } -# Function to select multiple options using spacebar +# Function to allow multiple selections using the spacebar function Select-MultipleOptions { param ( - [string]$prompt, - [array]$options + [string]$prompt, # The prompt message to display + [array]$options # The list of options to present to the user for multiple selection ) 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++) { - 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." - # Capture user input + # Capture user input for multiple selection while ($true) { - $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") - if ($key.Character -match '\d') { - $index = [int]$key.Character - 1 - if ($index -ge 0 -and $index -lt $options.Length) { + $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") # Read key input from the user + if ($key.Character -match '\d') { # Check if the input is a number + $index = [int]$key.Character - 1 # Convert character to index + if ($index -ge 0 -and $index -lt $options.Length) { # Validate index range + # Toggle selection state for the chosen option if ($selectedOptions.Contains($index)) { - $selectedOptions.Remove($index) + $selectedOptions.Remove($index) # Remove from selected options if already selected } 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 } } + # Return the list of selected 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") +# Handle the main menu choices switch ($mainMenuChoice) { 1 { - # Update all apps + # Option 1: Update 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 { - # 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") + # Handle the sub-menu choices for app installation switch ($installChoice) { 1 { - # Fresh install of ALL apps + # Sub-option 2a: 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 { - # Select package groups - $packageGroups = Get-ChildItem -Path "C:\winget\install\groups\*.ps1" | Select-Object -ExpandProperty Name + # Sub-option 2b: Select package groups for installation + # 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 + # Run the selected group scripts foreach ($group in $selectedGroups) { Write-Host "Installing group: $group" - & "C:\winget\install\groups\$group" + & "$currentDir\install\groups\$group" } } 3 { - # Install select packages only - $packages = Get-Content -Path "C:\winget\install\individual\list.ps1" + # Sub-option 2c: Install select packages only + # 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 + # Run the selected package installation scripts foreach ($package in $selectedPackages) { Write-Host "Installing package: $package" & $package @@ -93,6 +109,7 @@ switch ($mainMenuChoice) { } } default { + # Handle invalid choice Write-Host "Invalid choice, please try again." } }