Refactor Win32 app installation to process sources separately

Updates the Win32 app installation script to handle WinGet-sourced apps and user-defined apps in two separate batches instead of merging them. This ensures all WinGet apps are installed before any user-defined apps.

The core installation logic is extracted into a reusable function, improving script structure and readability.

Additionally, removes a pause command and a redundant console message from other scripts to improve automation flow.
This commit is contained in:
rbalsleyMSFT
2025-07-25 18:35:19 -07:00
parent b4f1985c99
commit 30c7f6f705
3 changed files with 87 additions and 66 deletions
@@ -266,5 +266,4 @@ foreach ($appFolder in Get-ChildItem -Path $basePath -Directory) {
# Final cleanup # Final cleanup
Write-Host "Installation process finished." Write-Host "Installation process finished."
pause
Remove-TemporaryFiles Remove-TemporaryFiles
@@ -63,69 +63,24 @@ function Invoke-Process {
return $cmd return $cmd
} }
# Define paths for the JSON files function Install-Applications {
$wingetAppsJsonFile = "$PSScriptRoot\WinGetWin32Apps.json" param(
# Look for UserAppList.json one directory level up from the script's location. This keeps the user specific json files (AppList.json and UserAppList.json in the Apps dir) [Parameter(Mandatory)]
$userAppsJsonFile = Join-Path -Path (Split-Path -Parent $PSScriptRoot) -ChildPath "UserAppList.json" [array]$apps
)
# Initialize an empty array to hold all apps if ($apps.Count -eq 0) {
$allApps = @() Write-Host "No applications to install from this source."
return
# Read the WinGetWin32Apps.json file if it exists
if (Test-Path -Path $wingetAppsJsonFile) {
Write-Host "Processing WinGetWin32Apps.json first..."
try {
$wingetApps = Get-Content -Path $wingetAppsJsonFile -Raw -ErrorAction Stop | ConvertFrom-Json
if ($wingetApps -is [array]) {
$allApps += $wingetApps
Write-Host "Found $(($wingetApps | Measure-Object).Count) WinGet Win32 apps."
} elseif ($wingetApps) {
$allApps += @($wingetApps) # Ensure it's added as an array element
Write-Host "Found 1 WinGet Win32 app."
} else {
Write-Host "WinGetWin32Apps.json is empty or invalid."
} }
} catch {
Write-Error "Failed to read or parse WinGetWin32Apps.json file: $_"
}
} else {
Write-Host "WinGetWin32Apps.json file not found. Skipping."
}
# Read the UserAppList.json file if it exists Write-Host "Total apps to install from this source: $($apps.Count)"
if (Test-Path -Path $userAppsJsonFile) {
Write-Host "Processing UserAppList.json next..."
try {
$userApps = Get-Content -Path $userAppsJsonFile -Raw -ErrorAction Stop | ConvertFrom-Json
if ($userApps -is [array]) {
$allApps += $userApps
Write-Host "Found $(($userApps | Measure-Object).Count) user-defined apps."
} elseif ($userApps) {
$allApps += @($userApps) # Ensure it's added as an array element
Write-Host "Found 1 user-defined app."
} else {
Write-Host "UserAppList.json is empty or invalid."
}
} catch {
Write-Error "Failed to read or parse UserAppList.json file: $_"
}
} else {
Write-Host "UserAppList.json file not found. Skipping."
}
# Check if there are any apps to install # Sort all apps by priority
if ($allApps.Count -eq 0) { $sortedApps = $apps | Sort-Object -Property Priority
Write-Host "No Win32 apps found in either WinGetWin32Apps.json or UserAppList.json. Exiting."
exit 0
}
Write-Host "Total apps to install: $($allApps.Count)" # Install each app
foreach ($app in $sortedApps) {
# Sort all apps by priority
$sortedApps = $allApps | Sort-Object -Property Priority
# Install each app
foreach ($app in $sortedApps) {
# Check if required properties exist # Check if required properties exist
if (-not $app.PSObject.Properties['Name'] -or -not $app.PSObject.Properties['CommandLine'] -or -not $app.PSObject.Properties['Arguments']) { if (-not $app.PSObject.Properties['Name'] -or -not $app.PSObject.Properties['CommandLine'] -or -not $app.PSObject.Properties['Arguments']) {
Write-Warning "Skipping app due to missing required properties (Name, CommandLine, Arguments): $($app | ConvertTo-Json -Depth 1 -Compress)" Write-Warning "Skipping app due to missing required properties (Name, CommandLine, Arguments): $($app | ConvertTo-Json -Depth 1 -Compress)"
@@ -165,6 +120,74 @@ foreach ($app in $sortedApps) {
} catch { } catch {
Write-Error "Error occurred while installing $($app.Name): $_" Write-Error "Error occurred while installing $($app.Name): $_"
} }
}
}
# Define paths for the JSON files
$wingetAppsJsonFile = "$PSScriptRoot\WinGetWin32Apps.json"
# Look for UserAppList.json one directory level up from the script's location. This keeps the user specific json files (AppList.json and UserAppList.json in the Apps dir)
$userAppsJsonFile = Join-Path -Path (Split-Path -Parent $PSScriptRoot) -ChildPath "UserAppList.json"
# Initialize empty arrays for apps from each source
$wingetApps = @()
$userApps = @()
# Read the WinGetWin32Apps.json file if it exists
if (Test-Path -Path $wingetAppsJsonFile) {
Write-Host "Processing WinGetWin32Apps.json..."
try {
$wingetContent = Get-Content -Path $wingetAppsJsonFile -Raw -ErrorAction Stop | ConvertFrom-Json
if ($wingetContent -is [array]) {
$wingetApps = $wingetContent
Write-Host "Found $(($wingetApps | Measure-Object).Count) WinGet Win32 apps."
} elseif ($wingetContent) {
$wingetApps = @($wingetContent) # Ensure it's an array
Write-Host "Found 1 WinGet Win32 app."
} else {
Write-Host "WinGetWin32Apps.json is empty or invalid."
}
} catch {
Write-Error "Failed to read or parse WinGetWin32Apps.json file: $_"
}
} else {
Write-Host "WinGetWin32Apps.json file not found. Skipping."
}
# Install WinGet apps if any were found
if ($wingetApps.Count -gt 0) {
Install-Applications -apps $wingetApps
}
# Read the UserAppList.json file if it exists
if (Test-Path -Path $userAppsJsonFile) {
Write-Host "Processing UserAppList.json..."
try {
$userContent = Get-Content -Path $userAppsJsonFile -Raw -ErrorAction Stop | ConvertFrom-Json
if ($userContent -is [array]) {
$userApps = $userContent
Write-Host "Found $(($userApps | Measure-Object).Count) user-defined apps."
} elseif ($userContent) {
$userApps = @($userContent) # Ensure it's an array
Write-Host "Found 1 user-defined app."
} else {
Write-Host "UserAppList.json is empty or invalid."
}
} catch {
Write-Error "Failed to read or parse UserAppList.json file: $_"
}
} else {
Write-Host "UserAppList.json file not found. Skipping."
}
# Install User apps if any were found
if ($userApps.Count -gt 0) {
Install-Applications -apps $userApps
}
# Check if any apps were installed at all
if ($wingetApps.Count -eq 0 -and $userApps.Count -eq 0) {
Write-Host "No Win32 apps found in either WinGetWin32Apps.json or UserAppList.json. Exiting."
exit 0
} }
Write-Host "All Win32 app installations attempted." Write-Host "All Win32 app installations attempted."
@@ -682,7 +682,6 @@ if ($null -eq $DriverSourcePath) {
} }
else { else {
WriteLog "Drivers folder not found at $DriversPath. Skipping driver installation." WriteLog "Drivers folder not found at $DriversPath. Skipping driver installation."
Write-Host "Drivers folder not found at $DriversPath. Skipping driver installation."
} }
} }
#Partition drive #Partition drive