winget/InstallApps.ps1

137 lines
5.4 KiB
PowerShell
Raw Permalink Normal View History

2024-09-07 19:08:42 +00:00
# Get the current directory where the script is located
$currentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
2024-09-08 02:57:43 +00:00
# Define the path for logging installed applications
$logFilePath = "/winget/installedApps.txt"
2024-09-07 19:04:50 +00:00
# Function to display a menu and get user input
function Show-Menu {
param (
2024-09-07 19:08:42 +00:00
[string]$prompt, # The prompt message to display
[array]$options # The list of options to present to the user
2024-09-07 19:04:50 +00:00
)
2024-09-07 19:08:42 +00:00
# Display the prompt and options
2024-09-07 19:04:50 +00:00
Write-Host $prompt
for ($i = 0; $i -lt $options.Length; $i++) {
2024-09-07 19:08:42 +00:00
Write-Host "$($i + 1). $($options[$i])" # Display each option with a number
2024-09-07 19:04:50 +00:00
}
2024-09-07 19:08:42 +00:00
# Get the user's choice
2024-09-07 19:04:50 +00:00
$choice = Read-Host "Select an option (1-$($options.Length))"
return $choice
}
2024-09-07 19:08:42 +00:00
# Function to allow multiple selections using the spacebar
2024-09-07 19:04:50 +00:00
function Select-MultipleOptions {
param (
2024-09-07 19:08:42 +00:00
[string]$prompt, # The prompt message to display
[array]$options # The list of options to present to the user for multiple selection
2024-09-07 19:04:50 +00:00
)
Write-Host $prompt
2024-09-07 19:08:42 +00:00
$selectedOptions = @() # Array to hold the selected options
2024-09-07 19:04:50 +00:00
2024-09-07 19:08:42 +00:00
# Display options with selection state (checked/unchecked)
2024-09-07 19:04:50 +00:00
for ($i = 0; $i -lt $options.Length; $i++) {
2024-09-07 19:08:42 +00:00
Write-Host "$($i + 1). $($options[$i]) [ ]" # Initial unchecked state for each option
2024-09-07 19:04:50 +00:00
}
Write-Host "Use spacebar to select/unselect and Enter to confirm."
2024-09-07 19:08:42 +00:00
# Capture user input for multiple selection
2024-09-07 19:04:50 +00:00
while ($true) {
2024-09-07 19:08:42 +00:00
$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
2024-09-07 19:04:50 +00:00
if ($selectedOptions.Contains($index)) {
2024-09-07 19:08:42 +00:00
$selectedOptions.Remove($index) # Remove from selected options if already selected
2024-09-07 19:04:50 +00:00
} else {
2024-09-07 19:08:42 +00:00
$selectedOptions.Add($index) # Add to selected options if not already selected
2024-09-07 19:04:50 +00:00
}
}
2024-09-07 19:08:42 +00:00
} elseif ($key.VirtualKeyCode -eq 13) { # Enter key to confirm selection
2024-09-07 19:04:50 +00:00
break
}
}
2024-09-07 19:08:42 +00:00
# Return the list of selected options
2024-09-07 19:04:50 +00:00
return $selectedOptions | ForEach-Object { $options[$_] }
}
2024-09-08 02:57:43 +00:00
# Function to log installed applications
function Log-InstalledApp {
param (
[string]$appName # The name of the installed application
)
# Append the application name to the log file
Add-Content -Path $logFilePath -Value $appName
}
2024-09-07 19:08:42 +00:00
# Main script logic starts here
2024-09-07 19:04:50 +00:00
$mainMenuChoice = Show-Menu "Do you want to:" @("Update all apps", "Install apps")
2024-09-07 19:08:42 +00:00
# Handle the main menu choices
2024-09-07 19:04:50 +00:00
switch ($mainMenuChoice) {
1 {
2024-09-07 19:08:42 +00:00
# Option 1: Update all apps
2024-09-07 19:04:50 +00:00
Write-Host "Updating all apps..."
2024-09-07 19:08:42 +00:00
Start-Process winget -ArgumentList "upgrade --all" -NoNewWindow -Wait # Run winget upgrade command
2024-09-07 19:04:50 +00:00
}
2 {
2024-09-07 19:08:42 +00:00
# Option 2: Install apps
2024-09-07 19:04:50 +00:00
$installChoice = Show-Menu "Choose an installation option:" @("Fresh install of ALL apps", "Select package groups", "Install select packages only")
2024-09-07 19:08:42 +00:00
# Handle the sub-menu choices for app installation
2024-09-07 19:04:50 +00:00
switch ($installChoice) {
1 {
2024-09-07 19:08:42 +00:00
# Sub-option 2a: Fresh install of ALL apps
2024-09-07 19:04:50 +00:00
Write-Host "Performing fresh install of all apps..."
2024-09-07 19:08:42 +00:00
# Execute all scripts in the groups folder
2024-09-08 02:57:43 +00:00
Get-ChildItem -Path "$currentDir\install\groups\*.ps1" | ForEach-Object {
& $_.FullName
# Log the installed app (assuming script file names are app names)
Log-InstalledApp -appName $_.Name
}
2024-09-07 19:04:50 +00:00
}
2 {
2024-09-07 19:08:42 +00:00
# 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
2024-09-07 19:04:50 +00:00
$selectedGroups = Select-MultipleOptions "Select package groups to install:" $packageGroups
2024-09-07 19:08:42 +00:00
# Run the selected group scripts
2024-09-07 19:04:50 +00:00
foreach ($group in $selectedGroups) {
Write-Host "Installing group: $group"
2024-09-07 19:08:42 +00:00
& "$currentDir\install\groups\$group"
2024-09-08 02:57:43 +00:00
# Log the installed group (assuming group script names are group names)
Log-InstalledApp -appName $group
2024-09-07 19:04:50 +00:00
}
}
3 {
2024-09-07 19:08:42 +00:00
# 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
2024-09-07 19:04:50 +00:00
$selectedPackages = Select-MultipleOptions "Select individual packages to install:" $packages
2024-09-07 19:08:42 +00:00
# Run the selected package installation scripts
2024-09-07 19:04:50 +00:00
foreach ($package in $selectedPackages) {
Write-Host "Installing package: $package"
& $package
2024-09-08 02:57:43 +00:00
# Log the installed package
Log-InstalledApp -appName $package
2024-09-07 19:04:50 +00:00
}
}
}
}
default {
2024-09-07 19:08:42 +00:00
# Handle invalid choice
2024-09-07 19:04:50 +00:00
Write-Host "Invalid choice, please try again."
}
}