mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Feat: Automate driver selection during FFU deployment
Adds a `DriverMapping.json` file to automate driver injection during image deployment. Driver download tasks now generate or update this mapping file with the relative path for each successfully downloaded driver package. The deployment script now uses this file to automatically detect and select the correct drivers for the target hardware, removing the need for manual selection. The manual driver selection prompt is retained as a fallback.
This commit is contained in:
@@ -80,8 +80,95 @@ function Compress-DriverFolderToWim {
|
||||
}
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# SECTION: Driver Mapping Function
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
function Update-DriverMappingJson {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[array]$DownloadedDrivers, # Array of PSCustomObjects with Make, Model, DriverPath
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$DriversFolder # Base drivers folder (e.g., C:\FFUDevelopment\Drivers)
|
||||
)
|
||||
|
||||
$mappingFilePath = Join-Path -Path $DriversFolder -ChildPath "DriverMapping.json"
|
||||
WriteLog "Updating driver mapping file at: $mappingFilePath"
|
||||
|
||||
# Load existing mapping file or create a new list
|
||||
$mappingList = [System.Collections.Generic.List[PSCustomObject]]::new()
|
||||
if (Test-Path -Path $mappingFilePath -PathType Leaf) {
|
||||
try {
|
||||
$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)
|
||||
}
|
||||
else {
|
||||
$mappingList.Add($existingJson)
|
||||
}
|
||||
WriteLog "Loaded $($mappingList.Count) existing entries from $mappingFilePath"
|
||||
}
|
||||
catch {
|
||||
WriteLog "Warning: Could not read or parse existing DriverMapping.json. A new file will be created. Error: $($_.Exception.Message)"
|
||||
}
|
||||
}
|
||||
|
||||
$updatedCount = 0
|
||||
$addedCount = 0
|
||||
|
||||
foreach ($driver in $DownloadedDrivers) {
|
||||
# Skip if any required property is missing or null
|
||||
if (-not $driver.PSObject.Properties['Make'] -or -not $driver.PSObject.Properties['Model'] -or -not $driver.PSObject.Properties['DriverPath'] -or [string]::IsNullOrWhiteSpace($driver.DriverPath)) {
|
||||
WriteLog "Skipping driver entry due to missing or empty Make, Model, or DriverPath. Details: $(($driver | ConvertTo-Json -Compress -Depth 3))"
|
||||
continue
|
||||
}
|
||||
|
||||
# Find existing entry
|
||||
$existingEntry = $mappingList | Where-Object { $_.Manufacturer -eq $driver.Make -and $_.Model -eq $driver.Model } | Select-Object -First 1
|
||||
|
||||
if ($null -ne $existingEntry) {
|
||||
# Update existing entry if the path is different
|
||||
if ($existingEntry.DriverPath -ne $driver.DriverPath) {
|
||||
WriteLog "Updating driver path for '$($driver.Make) - $($driver.Model)' from '$($existingEntry.DriverPath)' to '$($driver.DriverPath)'."
|
||||
$existingEntry.DriverPath = $driver.DriverPath
|
||||
$updatedCount++
|
||||
}
|
||||
}
|
||||
else {
|
||||
# Add new entry
|
||||
$newEntry = [PSCustomObject]@{
|
||||
Manufacturer = $driver.Make
|
||||
Model = $driver.Model
|
||||
DriverPath = $driver.DriverPath
|
||||
}
|
||||
$mappingList.Add($newEntry)
|
||||
WriteLog "Adding new mapping for '$($driver.Make) - $($driver.Model)' with path '$($driver.DriverPath)'."
|
||||
$addedCount++
|
||||
}
|
||||
}
|
||||
|
||||
if ($updatedCount -gt 0 -or $addedCount -gt 0) {
|
||||
try {
|
||||
# Sort the list for consistency before saving
|
||||
$sortedList = $mappingList | Sort-Object -Property Manufacturer, Model
|
||||
$sortedList | ConvertTo-Json -Depth 5 | Set-Content -Path $mappingFilePath -Encoding UTF8
|
||||
WriteLog "Successfully saved DriverMapping.json with $addedCount new entries and $updatedCount updated entries."
|
||||
}
|
||||
catch {
|
||||
WriteLog "Error saving updated DriverMapping.json: $($_.Exception.Message)"
|
||||
throw "Failed to save driver mapping file."
|
||||
}
|
||||
}
|
||||
else {
|
||||
WriteLog "No changes needed for DriverMapping.json."
|
||||
}
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# SECTION: Module Export
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
Export-ModuleMember -Function Compress-DriverFolderToWim
|
||||
Export-ModuleMember -Function Compress-DriverFolderToWim, Update-DriverMappingJson
|
||||
Reference in New Issue
Block a user