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 { else {
#Use cached vhdx file #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)" WriteLog "VHDX file is: $($cachedVHDXInfo.VhdxFileName)"
Robocopy.exe $($VHDXCacheFolder) $($VMPath) $($cachedVHDXInfo.VhdxFileName) /E /COPY:DAT /R:5 /W:5 /J 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 $existingJson = Get-Content -Path $mappingFilePath -Raw | ConvertFrom-Json
# Ensure it's a collection before adding to the list # Ensure it's a collection before adding to the list
if ($existingJson -is [array]) { 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 { else {
$mappingList.Add($existingJson) $mappingList.Add($existingJson)
@@ -179,12 +179,16 @@ function Save-DellDriversTask {
$driverRelativePath = Join-Path -Path $make -ChildPath $modelName # Relative path for the driver folder $driverRelativePath = Join-Path -Path $make -ChildPath $modelName # Relative path for the driver folder
try { try {
# Define paths for Dell catalog. The catalog is assumed to be prepared by the calling function. # Check if WIM file or driver folder already exist
$dellDriversFolder = Join-Path -Path $DriversFolder -ChildPath "Dell" $wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($modelName).wim"
$catalogBaseName = if ($WindowsRelease -le 11) { "CatalogPC" } else { "Catalog" } if (Test-Path -Path $wimFilePath -PathType Leaf) {
$dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml" $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) { if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum $folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) { 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* # 3. Parse the *EXISTING* XML and Find Drivers for *this specific model*
$status = "Finding drivers..." $status = "Finding drivers..."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status } 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..." } 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 # Ensure the base HP folder exists
if (-not (Test-Path -Path $hpDriversBaseFolder -PathType Container)) { if (-not (Test-Path -Path $hpDriversBaseFolder -PathType Container)) {
try { try {
@@ -142,18 +152,13 @@ function Save-HPDriversTask {
if ($CompressToWim) { if ($CompressToWim) {
$wimFilePath = Join-Path -Path $hpDriversBaseFolder -ChildPath "$($identifier).wim" # WIM in base HP folder, next to model folder $wimFilePath = Join-Path -Path $hpDriversBaseFolder -ChildPath "$($identifier).wim" # WIM in base HP folder, next to model folder
WriteLog "Attempting compression of existing folder '$modelSpecificFolder' to '$wimFilePath'."
if (Test-Path -Path $wimFilePath -PathType Leaf) {
$finalStatus = "Already downloaded (WIM exists)"
WriteLog "WIM file $wimFilePath already exists for $identifier."
}
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..." } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Compressing existing HP drivers for $identifier..." }
try { try {
Compress-DriverFolderToWim -SourceFolderPath $modelSpecificFolder -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop Compress-DriverFolderToWim -SourceFolderPath $modelSpecificFolder -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop
$finalStatus = "Already downloaded & Compressed" $finalStatus = "Already downloaded & Compressed"
WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath." WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath."
$driverRelativePath = Join-Path -Path $make -ChildPath "$($identifier).wim"
} }
catch { catch {
$errMsgForLog = "Error compressing existing drivers for $($identifier): $($_.Exception.Message)" $errMsgForLog = "Error compressing existing drivers for $($identifier): $($_.Exception.Message)"
@@ -162,15 +167,11 @@ function Save-HPDriversTask {
# $successState = false # Keep true if folder exists, compression is secondary # $successState = false # Keep true if folder exists, compression is secondary
} }
} }
}
else { else {
# Not compressing # Not compressing
$finalStatus = "Already downloaded" $finalStatus = "Already downloaded"
} }
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus }
if ($CompressToWim) {
$driverRelativePath = Join-Path -Path $make -ChildPath "$($identifier).wim"
}
return [PSCustomObject]@{ return [PSCustomObject]@{
Identifier = $identifier Identifier = $identifier
Status = $finalStatus Status = $finalStatus
@@ -96,6 +96,17 @@ function Save-LenovoDriversTask {
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Checking..." } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Checking..." }
try { 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) # 1. Check if drivers already exist for this model (final destination)
if (Test-Path -Path $modelPath -PathType Container) { if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum $folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
@@ -379,7 +390,7 @@ function Save-LenovoDriversTask {
if ($CompressToWim) { if ($CompressToWim) {
$status = "Compressing..." $status = "Compressing..."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $status } 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 $destinationWimPath = Join-Path -Path $makeDriversPath -ChildPath $wimFileName
$driverRelativePath = Join-Path -Path $make -ChildPath $wimFileName # Update relative path to the WIM file $driverRelativePath = Join-Path -Path $make -ChildPath $wimFileName # Update relative path to the WIM file
WriteLog "Compressing '$modelPath' to '$destinationWimPath'..." WriteLog "Compressing '$modelPath' to '$destinationWimPath'..."
@@ -102,23 +102,28 @@ function Save-MicrosoftDriversTask {
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Checking..." } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Checking..." }
try { 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 $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 $modelPath = Join-Path -Path $makeDriversPath -ChildPath $modelName
if (Test-Path -Path $modelPath -PathType Container) { if (Test-Path -Path $modelPath -PathType Container) {
$folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum $folderSize = (Get-ChildItem -Path $modelPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) { if ($folderSize -gt 1MB) {
$status = "Already downloaded" $status = "Already downloaded"
WriteLog "Drivers for '$modelName' already exist in '$modelPath'." 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 } 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 } return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $driverRelativePath }
} }
else { 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." WriteLog "Driver folder '$modelPath' for '$modelName' exists but is empty or very small. Re-downloading."
# Allow the process to continue to re-download
} }
} }