Improves driver matching to select the most specific rule

Refines the driver selection logic to correctly handle overlapping or wildcard-based rules in the driver mapping file.

Previously, the script would use the first rule that matched the system manufacturer and model. This could lead to a less specific rule being chosen if it appeared before a more specific one.

The logic now finds all matching rules and selects the one with the most specific model name, ensuring a more accurate driver package is applied.
This commit is contained in:
rbalsleyMSFT
2025-06-27 10:57:33 -07:00
parent dcb7957d15
commit 660a619944
@@ -436,16 +436,26 @@ if (Test-Path -Path $driverMappingPath -PathType Leaf) {
# Load and parse the mapping file
$driverMappings = Get-Content -Path $driverMappingPath -Raw | ConvertFrom-Json
# Find a matching rule
$matchedRule = $null
# Find all matching rules and select the most specific one
$matchingRules = @()
foreach ($rule in $driverMappings) {
# Use -like for wildcard matching
if ($systemManufacturer -like "$($rule.Manufacturer)*" -and $systemModel -like "$($rule.Model)*") {
$matchedRule = $rule
break
$matchingRules += $rule
}
}
# Select the best match
$matchedRule = $null
if ($matchingRules.Count -gt 0) {
WriteLog "Found $($matchingRules.Count) potential driver mapping rule(s)."
foreach ($rule in $matchingRules) {
WriteLog " - Potential Match: Manufacturer='$($rule.Manufacturer)', Model='$($rule.Model)', Path='$($rule.DriverPath)'"
}
# Sort by model name length, descending, to find the most specific match
$matchedRule = $matchingRules | Sort-Object -Property @{Expression = { $_.Model.Length } } -Descending | Select-Object -First 1
}
if ($null -ne $matchedRule) {
WriteLog "Automatic match found: Manufacturer='$($matchedRule.Manufacturer)', Model='$($matchedRule.Model)'"
$potentialDriverPath = Join-Path -Path $DriversPath -ChildPath $matchedRule.DriverPath