Optimize driver download by checking for existing WIM files

Refactors the driver download logic for all manufacturers to first check for the existence of a final `.wim` archive. If a WIM file is found, the download and processing for that model is skipped, significantly improving performance on subsequent runs.

This change also resolves a potential type conversion error when processing driver mapping JSON files and corrects a minor typo in a log message.
This commit is contained in:
rbalsleyMSFT
2025-06-27 12:51:51 -07:00
parent 660a619944
commit 1b0c0da677
6 changed files with 63 additions and 34 deletions
+1 -1
View File
@@ -4978,7 +4978,7 @@ try {
}
else {
#Use cached vhdx file
WriteLog 'Using cached VHDX file to speed up build proces'
WriteLog 'Using cached VHDX file to speed up build process'
WriteLog "VHDX file is: $($cachedVHDXInfo.VhdxFileName)"
Robocopy.exe $($VHDXCacheFolder) $($VMPath) $($cachedVHDXInfo.VhdxFileName) /E /COPY:DAT /R:5 /W:5 /J
@@ -116,7 +116,10 @@ function Update-DriverMappingJson {
$existingJson = Get-Content -Path $mappingFilePath -Raw | ConvertFrom-Json
# Ensure it's a collection before adding to the list
if ($existingJson -is [array]) {
$mappingList.AddRange($existingJson)
# Iterate through the array to avoid type conversion issues with AddRange
foreach ($item in $existingJson) {
$mappingList.Add($item)
}
}
else {
$mappingList.Add($existingJson)
@@ -179,12 +179,16 @@ function Save-DellDriversTask {
$driverRelativePath = Join-Path -Path $make -ChildPath $modelName # Relative path for the driver folder
try {
# Define paths for Dell catalog. The catalog is assumed to be prepared by the calling function.
$dellDriversFolder = Join-Path -Path $DriversFolder -ChildPath "Dell"
$catalogBaseName = if ($WindowsRelease -le 11) { "CatalogPC" } else { "Catalog" }
$dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml"
# Check if WIM file or driver folder already exist
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($modelName).wim"
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$status = "Already downloaded (WIM)"
WriteLog "Driver WIM for '$modelName' already exists at '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
$wimRelativePath = Join-Path -Path $make -ChildPath "$($modelName).wim"
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $wimRelativePath }
}
# 1. Check if drivers already exist for this model (final destination)
if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) {
@@ -198,6 +202,11 @@ function Save-DellDriversTask {
}
}
# Define paths for Dell catalog. The catalog is assumed to be prepared by the calling function.
$dellDriversFolder = Join-Path -Path $DriversFolder -ChildPath "Dell"
$catalogBaseName = if ($WindowsRelease -le 11) { "CatalogPC" } else { "Catalog" }
$dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml"
# 3. Parse the *EXISTING* XML and Find Drivers for *this specific model*
$status = "Finding drivers..."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
@@ -121,6 +121,16 @@ function Save-HPDriversTask {
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Checking HP drivers for $modelName..." }
# Check if WIM file already exists
$wimFilePath = Join-Path -Path $hpDriversBaseFolder -ChildPath "$($identifier).wim"
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$finalStatus = "Already downloaded (WIM)"
WriteLog "Driver WIM for '$identifier' already exists at '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus }
$wimRelativePath = Join-Path -Path $make -ChildPath "$($identifier).wim"
return [PSCustomObject]@{ Identifier = $identifier; Status = $finalStatus; Success = $true; DriverPath = $wimRelativePath }
}
# Ensure the base HP folder exists
if (-not (Test-Path -Path $hpDriversBaseFolder -PathType Container)) {
try {
@@ -142,25 +152,19 @@ function Save-HPDriversTask {
if ($CompressToWim) {
$wimFilePath = Join-Path -Path $hpDriversBaseFolder -ChildPath "$($identifier).wim" # WIM in base HP folder, next to model folder
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$finalStatus = "Already downloaded (WIM exists)"
WriteLog "WIM file $wimFilePath already exists for $identifier."
WriteLog "Attempting compression of existing folder '$modelSpecificFolder' to '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Compressing existing HP drivers for $identifier..." }
try {
Compress-DriverFolderToWim -SourceFolderPath $modelSpecificFolder -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop
$finalStatus = "Already downloaded & Compressed"
WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath."
$driverRelativePath = Join-Path -Path $make -ChildPath "$($identifier).wim"
}
else {
WriteLog "WIM file $wimFilePath not found for $identifier. Attempting compression of existing folder '$modelSpecificFolder'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Compressing existing HP drivers for $identifier..." }
try {
Compress-DriverFolderToWim -SourceFolderPath $modelSpecificFolder -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop
$finalStatus = "Already downloaded & Compressed"
WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath."
}
catch {
$errMsgForLog = "Error compressing existing drivers for $($identifier): $($_.Exception.Message)"
WriteLog $errMsgForLog
$finalStatus = "Already downloaded (Compression failed: $($_.Exception.Message.Split([Environment]::NewLine)[0]))"
# $successState = false # Keep true if folder exists, compression is secondary
}
catch {
$errMsgForLog = "Error compressing existing drivers for $($identifier): $($_.Exception.Message)"
WriteLog $errMsgForLog
$finalStatus = "Already downloaded (Compression failed: $($_.Exception.Message.Split([Environment]::NewLine)[0]))"
# $successState = false # Keep true if folder exists, compression is secondary
}
}
else {
@@ -168,9 +172,6 @@ function Save-HPDriversTask {
$finalStatus = "Already downloaded"
}
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus }
if ($CompressToWim) {
$driverRelativePath = Join-Path -Path $make -ChildPath "$($identifier).wim"
}
return [PSCustomObject]@{
Identifier = $identifier
Status = $finalStatus
@@ -96,6 +96,17 @@ function Save-LenovoDriversTask {
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Checking..." }
try {
# Check if WIM file or driver folder already exist
$sanitizedIdentifier = $identifier -replace '[\\/:"*?<>|]', '_'
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($sanitizedIdentifier).wim"
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$status = "Already downloaded (WIM)"
WriteLog "Driver WIM for '$identifier' already exists at '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $status }
$wimRelativePath = Join-Path -Path $make -ChildPath "$($sanitizedIdentifier).wim"
return [PSCustomObject]@{ Identifier = $identifier; Status = $status; Success = $true; DriverPath = $wimRelativePath }
}
# 1. Check if drivers already exist for this model (final destination)
if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
@@ -379,7 +390,7 @@ function Save-LenovoDriversTask {
if ($CompressToWim) {
$status = "Compressing..."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $status }
$wimFileName = "$($identifier).wim" # Use sanitized identifier for filename
$wimFileName = "$($sanitizedIdentifier).wim" # Use sanitized identifier for filename
$destinationWimPath = Join-Path -Path $makeDriversPath -ChildPath $wimFileName
$driverRelativePath = Join-Path -Path $make -ChildPath $wimFileName # Update relative path to the WIM file
WriteLog "Compressing '$modelPath' to '$destinationWimPath'..."
@@ -102,23 +102,28 @@ function Save-MicrosoftDriversTask {
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Checking..." }
try {
# Check if drivers already exist for this model
# Check if WIM file or driver folder already exist
$makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $Make
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($modelName).wim"
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$status = "Already downloaded (WIM)"
WriteLog "Driver WIM for '$modelName' already exists at '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
$wimRelativePath = Join-Path -Path $make -ChildPath "$($modelName).wim"
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $wimRelativePath }
}
$modelPath = Join-Path -Path $makeDriversPath -ChildPath $modelName
if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) {
$status = "Already downloaded"
WriteLog "Drivers for '$modelName' already exist in '$modelPath'."
# Enqueue this status before returning
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
# Return success immediately
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $driverRelativePath }
}
else {
# Status is not set to error here, just log and continue
WriteLog "Driver folder '$modelPath' for '$modelName' exists but is empty or very small. Re-downloading."
# Allow the process to continue to re-download
}
}