Feat: Auto-save selected drivers and refine script execution

Adds a feature to automatically save the selected drivers to `Drivers.json` upon download, preserving the user's selection for future use.

Improves script execution by:
- Ensuring the disk cleanup process completes before proceeding.
- Suppressing progress bars globally for a cleaner console output.
- Hiding non-critical warnings during driver installation.
- Removing an unused BITS transfer function and trailing whitespace.
This commit is contained in:
rbalsleyMSFT
2025-07-14 16:55:15 -07:00
parent 08c9d5a0e3
commit 21d5f74dd8
4 changed files with 118 additions and 49 deletions
@@ -11,7 +11,7 @@ Get-ChildItem -Path $rootKey | ForEach-Object {
}
# Run the disk cleanup tool with the specified flags
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:0"
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:0" -Wait
# Remove the StateFlags0000 registry values that were added
Get-ChildItem -Path $rootKey | ForEach-Object {
+37 -36
View File
@@ -403,6 +403,7 @@ param(
[string]$orchestrationPath,
[bool]$UpdateADK = $true
)
$ProgressPreference = 'SilentlyContinue'
$version = '2505.1'
# Remove any existing modules to avoid conflicts
@@ -704,40 +705,40 @@ function Test-Url {
}
# Function to download a file using BITS with retry and error handling
function Start-BitsTransferWithRetry {
param (
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Destination,
[int]$Retries = 3
)
# function Start-BitsTransferWithRetry {
# param (
# [Parameter(Mandatory = $true)]
# [string]$Source,
# [Parameter(Mandatory = $true)]
# [string]$Destination,
# [int]$Retries = 3
# )
$attempt = 0
$lastError = $null
# $attempt = 0
# $lastError = $null
while ($attempt -lt $Retries) {
try {
$OriginalVerbosePreference = $VerbosePreference
$VerbosePreference = 'SilentlyContinue'
$ProgressPreference = 'SilentlyContinue'
Start-BitsTransfer -Source $Source -Destination $Destination -ErrorAction Stop
$ProgressPreference = 'Continue'
$VerbosePreference = $OriginalVerbosePreference
return
}
catch {
# Capture the error that occurred during the failed download attempt
$lastError = $_
$attempt++
WriteLog "Attempt $attempt of $Retries failed to download $Source with error: $($lastError.Exception.Message). Retrying..."
Start-Sleep -Seconds 5
}
}
# while ($attempt -lt $Retries) {
# try {
# $OriginalVerbosePreference = $VerbosePreference
# $VerbosePreference = 'SilentlyContinue'
# # $ProgressPreference = 'SilentlyContinue'
# Start-BitsTransfer -Source $Source -Destination $Destination -ErrorAction Stop
# # $ProgressPreference = 'Continue'
# $VerbosePreference = $OriginalVerbosePreference
# return
# }
# catch {
# # Capture the error that occurred during the failed download attempt
# $lastError = $_
# $attempt++
# WriteLog "Attempt $attempt of $Retries failed to download $Source with error: $($lastError.Exception.Message). Retrying..."
# Start-Sleep -Seconds 5
# }
# }
WriteLog "Failed to download $Source after $Retries attempts. Error: $($lastError.Exception.Message)"
throw $lastError
}
# WriteLog "Failed to download $Source after $Retries attempts. Error: $($lastError.Exception.Message)"
# throw $lastError
# }
function Get-MicrosoftDrivers {
param (
@@ -925,9 +926,9 @@ function Get-MicrosoftDrivers {
elseif ($fileExtension -eq ".zip") {
# Extract the ZIP file
WriteLog "Extracting ZIP file to $modelPath"
$ProgressPreference = 'SilentlyContinue'
# $ProgressPreference = 'SilentlyContinue'
Expand-Archive -Path $filePath -DestinationPath $modelPath -Force
$ProgressPreference = 'Continue'
# $ProgressPreference = 'Continue'
WriteLog "Extraction complete"
}
else {
@@ -1946,7 +1947,7 @@ function Get-WindowsESD {
#Download if ESD file doesn't already exist
If (-not (Test-Path $esdFilePath)) {
#Required to fix slow downloads
$ProgressPreference = 'SilentlyContinue'
# $ProgressPreference = 'SilentlyContinue'
WriteLog "Downloading $($file.filePath) to $esdFIlePath"
$OriginalVerbosePreference = $VerbosePreference
$VerbosePreference = 'SilentlyContinue'
@@ -1954,7 +1955,7 @@ function Get-WindowsESD {
$VerbosePreference = $OriginalVerbosePreference
WriteLog "Download succeeded"
#Set back to show progress
$ProgressPreference = 'Continue'
# $ProgressPreference = 'Continue'
WriteLog "Cleanup cab and xml file"
Remove-Item -Path $cabFilePath -Force
Remove-Item -Path $xmlFilePath -Force
@@ -2895,7 +2896,7 @@ function New-FFU {
WriteLog 'Mounting complete'
WriteLog 'Adding drivers - This will take a few minutes, please be patient'
try {
Add-WindowsDriver -Path "$FFUDevelopmentPath\Mount" -Driver "$DriversFolder" -Recurse -ErrorAction SilentlyContinue | Out-null
Add-WindowsDriver -Path "$FFUDevelopmentPath\Mount" -Driver "$DriversFolder" -Recurse -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-null
}
catch {
WriteLog 'Some drivers failed to be added to the FFU. This can be expected. Continuing.'
@@ -257,4 +257,7 @@ function Set-Progress {
WriteLog "[PROGRESS] $Percentage | $Message"
}
# Suppress the default progress bar for a cleaner console output for any script importing this module.
# $ProgressPreference = 'SilentlyContinue'
Export-ModuleMember -Function *
@@ -695,6 +695,71 @@ function Invoke-DownloadSelectedDrivers {
}
}
# Automatically save the selected drivers to the specified Drivers.json path
$driversJsonPath = $State.Controls.txtDriversJsonPath.Text
if (-not [string]::IsNullOrWhiteSpace($driversJsonPath) -and $selectedDrivers.Count -gt 0) {
WriteLog "Attempting to automatically save selected drivers list to $driversJsonPath"
try {
$outputJson = @{} # Use a Hashtable for the desired structure
$selectedDrivers | Group-Object -Property Make | ForEach-Object {
$makeName = $_.Name
$modelsForThisMake = @() # Initialize an array to hold model objects
foreach ($driverItem in $_.Group) {
$modelObject = $null
switch ($makeName) {
'Microsoft' {
$modelObject = @{
Name = $driverItem.Model # Model is the display name
Link = $driverItem.Link
}
}
'Dell' {
$modelObject = @{
Name = $driverItem.Model # Model is the display name
}
}
'HP' {
$modelObject = @{
Name = $driverItem.Model
}
}
'Lenovo' {
$modelObject = @{
Name = $driverItem.Model
ProductName = $driverItem.ProductName
MachineType = $driverItem.MachineType
}
}
default {
WriteLog "Auto-Save Drivers.json: Unrecognized Make '$makeName' for driver '$($driverItem.Model)'. Skipping."
}
}
if ($null -ne $modelObject) {
$modelsForThisMake += $modelObject
}
}
# Add the models array to the make-specific object
$outputJson[$makeName] = @{ Models = $modelsForThisMake }
}
# Ensure directory exists
$parentDir = Split-Path -Path $driversJsonPath -Parent
if (-not (Test-Path -Path $parentDir -PathType Container)) {
WriteLog "Creating directory for Drivers.json: $parentDir"
New-Item -Path $parentDir -ItemType Directory -Force | Out-Null
}
$outputJson | ConvertTo-Json -Depth 5 | Set-Content -Path $driversJsonPath -Encoding UTF8
WriteLog "Successfully auto-saved selected drivers to $driversJsonPath"
}
catch {
WriteLog "Failed to automatically save selected drivers to $driversJsonPath. Error: $($_.Exception.Message)"
# This is a best-effort operation, so we only log the error and don't bother the user with a popup.
}
}
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
$Button.IsEnabled = $true
if ($overallSuccess) {