Feat: Add WIM compression for existing driver folders

Implements logic to compress previously downloaded driver folders into WIM files if the compression option is enabled. This ensures consistency in the driver store when re-running the download process.

Refactors the parallel task processing to rely on a standardized boolean 'Success' property from all driver download tasks. This simplifies the result handling logic and makes it more reliable than parsing status strings.
This commit is contained in:
rbalsleyMSFT
2025-06-28 00:51:22 -07:00
parent 0b0046986e
commit 1523091637
5 changed files with 99 additions and 12 deletions
@@ -218,22 +218,19 @@ function Invoke-ParallelProcessing {
elseif ($taskResult.PSObject.Properties.Name -contains 'Identifier') { $taskSpecificIdentifier = $taskResult.Identifier } elseif ($taskResult.PSObject.Properties.Name -contains 'Identifier') { $taskSpecificIdentifier = $taskResult.Identifier }
$resultStatus = $taskResult.Status $resultStatus = $taskResult.Status
# Simplified success check. All driver tasks should now return a 'Success' property.
if ($taskResult.PSObject.Properties.Name -contains 'Success') { if ($taskResult.PSObject.Properties.Name -contains 'Success') {
# Dell, Microsoft, Lenovo
$resultCode = if ($taskResult.Success) { 0 } else { 1 } $resultCode = if ($taskResult.Success) { 0 } else { 1 }
} }
elseif ($taskResult.Status -like 'Completed*') { else {
# HP success # Fallback for any task that *still* doesn't return 'Success'. This is now the exceptional case.
$resultCode = 0 WriteLog "Warning: Task for '$taskSpecificIdentifier' did not return a 'Success' property. Inferring from status: '$($taskResult.Status)'"
} if ($taskResult.Status -like 'Completed*' -or $taskResult.Status -like 'Already downloaded*') {
elseif ($taskResult.Status -like 'Error*') { $resultCode = 0 # Treat as success
# HP error
$resultCode = 1
} }
else { else {
# Default for HP if status is unexpected, or if 'Success' property is missing but status isn't 'Completed*' or 'Error*' $resultCode = 1 # Treat as error
WriteLog "Unexpected status or missing 'Success' property from task for '$taskSpecificIdentifier': $($taskResult.Status)" }
$resultCode = 1 # Assume error
} }
} }
elseif ($make -in ('Microsoft', 'Dell', 'HP', 'Lenovo')) { elseif ($make -in ('Microsoft', 'Dell', 'HP', 'Lenovo')) {
@@ -186,6 +186,33 @@ function Save-DellDriversTask {
if (-not $existingDriver.PSObject.Properties['Model']) { if (-not $existingDriver.PSObject.Properties['Model']) {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName $existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName
} }
# Special handling for existing folders that need compression
if ($CompressToWim -and $existingDriver.Status -eq 'Already downloaded') {
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($modelName).wim"
$sourceFolderPath = Join-Path -Path $makeDriversPath -ChildPath $modelName
WriteLog "Attempting compression of existing folder '$sourceFolderPath' to '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Compressing existing..." }
try {
Compress-DriverFolderToWim -SourceFolderPath $sourceFolderPath -DestinationWimPath $wimFilePath -WimName $modelName -WimDescription "Drivers for $modelName" -ErrorAction Stop
$existingDriver.Status = "Already downloaded & Compressed"
$existingDriver.DriverPath = Join-Path -Path $make -ChildPath "$($modelName).wim"
$existingDriver.Success = $true
WriteLog "Successfully compressed existing drivers for $modelName to $wimFilePath."
}
catch {
WriteLog "Error compressing existing drivers for $($modelName): $($_.Exception.Message)"
$existingDriver.Status = "Already downloaded (Compression failed)"
$existingDriver.Success = $false
}
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $existingDriver.Status }
}
# Ensure the Success property exists on the object being returned.
if (-not $existingDriver.PSObject.Properties.Name -contains 'Success') {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Success' -Value $true
}
return $existingDriver return $existingDriver
} }
@@ -141,14 +141,22 @@ function Save-HPDriversTask {
Compress-DriverFolderToWim -SourceFolderPath $sourceFolderPath -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop Compress-DriverFolderToWim -SourceFolderPath $sourceFolderPath -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop
$existingDriver.Status = "Already downloaded & Compressed" $existingDriver.Status = "Already downloaded & Compressed"
$existingDriver.DriverPath = Join-Path -Path $make -ChildPath "$($sanitizedModelName).wim" $existingDriver.DriverPath = Join-Path -Path $make -ChildPath "$($sanitizedModelName).wim"
$existingDriver.Success = $true
WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath." WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath."
} }
catch { catch {
WriteLog "Error compressing existing drivers for $($identifier): $($_.Exception.Message)" WriteLog "Error compressing existing drivers for $($identifier): $($_.Exception.Message)"
$existingDriver.Status = "Already downloaded (Compression failed)" $existingDriver.Status = "Already downloaded (Compression failed)"
$existingDriver.Success = $false
} }
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $existingDriver.Status } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $existingDriver.Status }
} }
# Ensure the Success property exists on the object being returned.
if (-not $existingDriver.PSObject.Properties.Name -contains 'Success') {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Success' -Value $true
}
return $existingDriver return $existingDriver
} }
@@ -104,6 +104,33 @@ function Save-LenovoDriversTask {
# We need to return 'Identifier' for Lenovo's logic. # We need to return 'Identifier' for Lenovo's logic.
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Identifier' -Value $identifier -Force $existingDriver | Add-Member -MemberType NoteProperty -Name 'Identifier' -Value $identifier -Force
$existingDriver.PSObject.Properties.Remove('Model') $existingDriver.PSObject.Properties.Remove('Model')
# Special handling for existing folders that need compression
if ($CompressToWim -and $existingDriver.Status -eq 'Already downloaded') {
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($sanitizedIdentifier).wim"
$sourceFolderPath = Join-Path -Path $makeDriversPath -ChildPath $sanitizedIdentifier
WriteLog "Attempting compression of existing folder '$sourceFolderPath' to '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Compressing existing..." }
try {
Compress-DriverFolderToWim -SourceFolderPath $sourceFolderPath -DestinationWimPath $wimFilePath -WimName $identifier -WimDescription "Drivers for $identifier" -ErrorAction Stop
$existingDriver.Status = "Already downloaded & Compressed"
$existingDriver.DriverPath = Join-Path -Path $make -ChildPath "$($sanitizedIdentifier).wim"
$existingDriver.Success = $true
WriteLog "Successfully compressed existing drivers for $identifier to $wimFilePath."
}
catch {
WriteLog "Error compressing existing drivers for $($identifier): $($_.Exception.Message)"
$existingDriver.Status = "Already downloaded (Compression failed)"
$existingDriver.Success = $false
}
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $existingDriver.Status }
}
# Ensure the Success property exists on the object being returned.
if (-not $existingDriver.PSObject.Properties.Name -contains 'Success') {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Success' -Value $true
}
return $existingDriver return $existingDriver
} }
@@ -109,6 +109,34 @@ function Save-MicrosoftDriversTask {
if (-not $existingDriver.PSObject.Properties['Model']) { if (-not $existingDriver.PSObject.Properties['Model']) {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName $existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName
} }
# Special handling for existing folders that need compression
if ($CompressToWim -and $existingDriver.Status -eq 'Already downloaded') {
$makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $make
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($modelName).wim"
$sourceFolderPath = Join-Path -Path $makeDriversPath -ChildPath $modelName
WriteLog "Attempting compression of existing folder '$sourceFolderPath' to '$wimFilePath'."
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Compressing existing..." }
try {
Compress-DriverFolderToWim -SourceFolderPath $sourceFolderPath -DestinationWimPath $wimFilePath -WimName $modelName -WimDescription "Drivers for $modelName" -ErrorAction Stop
$existingDriver.Status = "Already downloaded & Compressed"
$existingDriver.DriverPath = Join-Path -Path $make -ChildPath "$($modelName).wim"
$existingDriver.Success = $true
WriteLog "Successfully compressed existing drivers for $modelName to $wimFilePath."
}
catch {
WriteLog "Error compressing existing drivers for $($modelName): $($_.Exception.Message)"
$existingDriver.Status = "Already downloaded (Compression failed)"
$existingDriver.Success = $false
}
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $existingDriver.Status }
}
# Ensure the Success property exists on the object being returned.
if (-not $existingDriver.PSObject.Properties.Name -contains 'Success') {
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Success' -Value $true
}
return $existingDriver return $existingDriver
} }