mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Refactor driver existence check into a common function
Adds a new `Test-ExistingDriver` function to the common driver module to centralize the logic for checking if drivers have already been downloaded, either as a folder or a WIM file. This change removes duplicated code from the Dell, HP, Lenovo, and Microsoft driver download tasks, simplifying maintenance and ensuring consistent behavior across all manufacturers.
This commit is contained in:
@@ -182,8 +182,72 @@ function Update-DriverMappingJson {
|
||||
}
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# SECTION: Driver Existence Check Function
|
||||
# --------------------------------------------------------------------------
|
||||
function Test-ExistingDriver {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Make,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Model,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$DriversFolder,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Identifier,
|
||||
|
||||
[Parameter()]
|
||||
[System.Collections.Concurrent.ConcurrentQueue[hashtable]]$ProgressQueue = $null
|
||||
)
|
||||
|
||||
$makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $Make
|
||||
$modelPath = Join-Path -Path $makeDriversPath -ChildPath $Model
|
||||
$driverRelativePath = Join-Path -Path $Make -ChildPath $Model
|
||||
|
||||
# Check for WIM file first
|
||||
$wimFilePath = Join-Path -Path $makeDriversPath -ChildPath "$($Model).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 "$($Model).wim"
|
||||
return [PSCustomObject]@{
|
||||
Model = $Identifier # Return original identifier
|
||||
Status = $status
|
||||
Success = $true
|
||||
DriverPath = $wimRelativePath
|
||||
}
|
||||
}
|
||||
|
||||
# Check for existing driver folder
|
||||
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 '$Identifier' already exist in '$modelPath'."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $Identifier -Status $status }
|
||||
return [PSCustomObject]@{
|
||||
Model = $Identifier # Return original identifier
|
||||
Status = $status
|
||||
Success = $true
|
||||
DriverPath = $driverRelativePath
|
||||
}
|
||||
}
|
||||
else {
|
||||
WriteLog "Driver folder '$modelPath' for '$Identifier' exists but is empty or very small. Re-downloading."
|
||||
}
|
||||
}
|
||||
|
||||
# If neither WIM nor a valid folder exists, return null
|
||||
return $null
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# SECTION: Module Export
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
Export-ModuleMember -Function Compress-DriverFolderToWim, Update-DriverMappingJson
|
||||
Export-ModuleMember -Function Compress-DriverFolderToWim, Update-DriverMappingJson, Test-ExistingDriver
|
||||
@@ -179,27 +179,14 @@ function Save-DellDriversTask {
|
||||
$driverRelativePath = Join-Path -Path $make -ChildPath $modelName # Relative path for the driver folder
|
||||
|
||||
try {
|
||||
# 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 }
|
||||
}
|
||||
|
||||
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'."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $driverRelativePath }
|
||||
}
|
||||
else {
|
||||
WriteLog "Driver folder '$modelPath' for '$modelName' exists but is empty/small. Re-downloading."
|
||||
# Check for existing drivers
|
||||
$existingDriver = Test-ExistingDriver -Make $make -Model $modelName -DriversFolder $DriversFolder -Identifier $modelName -ProgressQueue $ProgressQueue
|
||||
if ($null -ne $existingDriver) {
|
||||
# Add the 'Model' property to the return object for consistency if it's not there
|
||||
if (-not $existingDriver.PSObject.Properties['Model']) {
|
||||
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName
|
||||
}
|
||||
return $existingDriver
|
||||
}
|
||||
|
||||
# Define paths for Dell catalog. The catalog is assumed to be prepared by the calling function.
|
||||
|
||||
@@ -112,79 +112,50 @@ function Save-HPDriversTask {
|
||||
$modelName = $DriverItemData.Model
|
||||
$make = $DriverItemData.Make # Should be 'HP'
|
||||
$identifier = $modelName # Unique identifier for progress updates
|
||||
$sanitizedModelName = $modelName -replace '[\\/:"*?<>|]', '_'
|
||||
$hpDriversBaseFolder = Join-Path -Path $DriversFolder -ChildPath $make # Changed variable name for clarity
|
||||
$platformListXml = Join-Path -Path $hpDriversBaseFolder -ChildPath "PlatformList.xml"
|
||||
$modelSpecificFolder = Join-Path -Path $hpDriversBaseFolder -ChildPath ($modelName -replace '[\\/:"*?<>|]', '_') # Sanitize model name for folder path
|
||||
$driverRelativePath = Join-Path -Path $make -ChildPath ($modelName -replace '[\\/:"*?<>|]', '_') # Relative path for the driver folder
|
||||
$modelSpecificFolder = Join-Path -Path $hpDriversBaseFolder -ChildPath $sanitizedModelName # Sanitize model name for folder path
|
||||
$driverRelativePath = Join-Path -Path $make -ChildPath $sanitizedModelName # Relative path for the driver folder
|
||||
$finalStatus = "" # Initialize final status
|
||||
$successState = $true # Assume success unless an operation fails
|
||||
|
||||
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 {
|
||||
New-Item -Path $hpDriversBaseFolder -ItemType Directory -Force -ErrorAction Stop | Out-Null
|
||||
WriteLog "Created base HP driver folder: $hpDriversBaseFolder"
|
||||
}
|
||||
catch {
|
||||
$errMsg = "Failed to create base HP driver folder '$hpDriversBaseFolder': $($_.Exception.Message)"
|
||||
WriteLog $errMsg
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Error: Create HP dir failed" }
|
||||
return [PSCustomObject]@{ Identifier = $identifier; Status = "Error: Create HP dir failed"; Success = $false; DriverPath = $null }
|
||||
}
|
||||
}
|
||||
|
||||
# Check if drivers already exist for this model
|
||||
if (Test-Path -Path $modelSpecificFolder -PathType Container) {
|
||||
WriteLog "HP drivers for '$identifier' already exist in '$modelSpecificFolder'."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Found existing HP drivers for $identifier. Verifying..." }
|
||||
|
||||
if ($CompressToWim) {
|
||||
$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 ($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"
|
||||
}
|
||||
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 {
|
||||
# Not compressing
|
||||
$finalStatus = "Already downloaded"
|
||||
}
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $finalStatus }
|
||||
return [PSCustomObject]@{
|
||||
Identifier = $identifier
|
||||
Status = $finalStatus
|
||||
Success = $successState
|
||||
DriverPath = $driverRelativePath
|
||||
}
|
||||
}
|
||||
|
||||
# If folder does not exist, proceed with download and extraction
|
||||
WriteLog "HP drivers for '$identifier' not found locally. Starting download process..."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Downloading HP drivers for $identifier..." }
|
||||
|
||||
try {
|
||||
# Check for existing drivers
|
||||
$existingDriver = Test-ExistingDriver -Make $make -Model $sanitizedModelName -DriversFolder $DriversFolder -Identifier $identifier -ProgressQueue $ProgressQueue
|
||||
if ($null -ne $existingDriver) {
|
||||
# The return object from Test-ExistingDriver uses 'Model' as the identifier key.
|
||||
# We need to return 'Identifier' for HP's logic.
|
||||
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Identifier' -Value $identifier -Force
|
||||
$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 $hpDriversBaseFolder -ChildPath "$($sanitizedModelName).wim"
|
||||
$sourceFolderPath = Join-Path -Path $hpDriversBaseFolder -ChildPath $sanitizedModelName
|
||||
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 "$($sanitizedModelName).wim"
|
||||
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)"
|
||||
}
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $existingDriver.Status }
|
||||
}
|
||||
return $existingDriver
|
||||
}
|
||||
|
||||
# If folder does not exist, proceed with download and extraction
|
||||
WriteLog "HP drivers for '$identifier' not found locally. Starting download process..."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status "Downloading..." }
|
||||
|
||||
# Ensure PlatformList.xml exists (it should have been downloaded by Get-HPDriversModelList)
|
||||
if (-not (Test-Path -Path $platformListXml)) {
|
||||
# Attempt to download/extract it again if missing
|
||||
|
||||
@@ -83,42 +83,28 @@ function Save-LenovoDriversTask {
|
||||
$identifier = $DriverItemData.Model
|
||||
$machineType = $DriverItemData.MachineType
|
||||
$make = "Lenovo"
|
||||
$sanitizedIdentifier = $identifier -replace '[\\/:"*?<>|]', '_'
|
||||
$status = "Starting..."
|
||||
$success = $false
|
||||
|
||||
# Define paths
|
||||
$makeDriversPath = Join-Path -Path $DriversFolder -ChildPath $Make
|
||||
# Use the identifier (which contains the model name and machine type) and sanitize it for the path
|
||||
$modelPath = Join-Path -Path $makeDriversPath -ChildPath ($identifier -replace '[\\/:"*?<>|]', '_')
|
||||
$driverRelativePath = Join-Path -Path $make -ChildPath ($identifier -replace '[\\/:"*?<>|]', '_') # Relative path for the driver folder
|
||||
$modelPath = Join-Path -Path $makeDriversPath -ChildPath $sanitizedIdentifier
|
||||
$driverRelativePath = Join-Path -Path $make -ChildPath $sanitizedIdentifier # Relative path for the driver folder
|
||||
$tempDownloadPath = Join-Path -Path $makeDriversPath -ChildPath "_TEMP_$($machineType)_$($PID)" # Temp folder for catalog/package XMLs
|
||||
|
||||
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
|
||||
if ($folderSize -gt 1MB) {
|
||||
$status = "Already downloaded"
|
||||
WriteLog "Drivers for '$identifier' already exist in '$modelPath'."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $identifier -Status $status }
|
||||
return [PSCustomObject]@{ Identifier = $identifier; Status = $status; Success = $true; DriverPath = $driverRelativePath }
|
||||
}
|
||||
else {
|
||||
WriteLog "Driver folder '$modelPath' for '$identifier' exists but is empty/small. Re-downloading."
|
||||
}
|
||||
# Check for existing drivers
|
||||
$existingDriver = Test-ExistingDriver -Make $make -Model $sanitizedIdentifier -DriversFolder $DriversFolder -Identifier $identifier -ProgressQueue $ProgressQueue
|
||||
if ($null -ne $existingDriver) {
|
||||
# The return object from Test-ExistingDriver uses 'Model' as the identifier key.
|
||||
# We need to return 'Identifier' for Lenovo's logic.
|
||||
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Identifier' -Value $identifier -Force
|
||||
$existingDriver.PSObject.Properties.Remove('Model')
|
||||
return $existingDriver
|
||||
}
|
||||
|
||||
# Ensure base directories exist
|
||||
|
||||
@@ -102,29 +102,14 @@ function Save-MicrosoftDriversTask {
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status "Checking..." }
|
||||
|
||||
try {
|
||||
# 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'."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $true; DriverPath = $driverRelativePath }
|
||||
}
|
||||
else {
|
||||
WriteLog "Driver folder '$modelPath' for '$modelName' exists but is empty or very small. Re-downloading."
|
||||
# Check for existing drivers
|
||||
$existingDriver = Test-ExistingDriver -Make $make -Model $modelName -DriversFolder $DriversFolder -Identifier $modelName -ProgressQueue $ProgressQueue
|
||||
if ($null -ne $existingDriver) {
|
||||
# Add the 'Model' property to the return object for consistency if it's not there
|
||||
if (-not $existingDriver.PSObject.Properties['Model']) {
|
||||
$existingDriver | Add-Member -MemberType NoteProperty -Name 'Model' -Value $modelName
|
||||
}
|
||||
return $existingDriver
|
||||
}
|
||||
|
||||
### GET THE DOWNLOAD LINK
|
||||
|
||||
Reference in New Issue
Block a user