mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Improves driver downloads by using cached links
Reduces unnecessary Download Center requests by preferring cached file details when available Falls back to downloading/parsing the page only on cache miss, then backfills the cache for future runs Adds error handling and logging around cache load/update to avoid download failures from cache issues
This commit is contained in:
@@ -185,6 +185,47 @@ function Save-MicrosoftDriversTask {
|
|||||||
### GET THE DOWNLOAD LINK
|
### GET THE DOWNLOAD LINK
|
||||||
$status = "Getting download link..."
|
$status = "Getting download link..."
|
||||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||||
|
|
||||||
|
# Initialize Win10/Win11 link variables
|
||||||
|
$win10Link = $null
|
||||||
|
$win10FileName = $null
|
||||||
|
$win11Link = $null
|
||||||
|
$win11FileName = $null
|
||||||
|
|
||||||
|
# Prefer cached Download Center details (Source C) to avoid unnecessary internet calls and cache rewrites
|
||||||
|
$useCachedDownloadCenterDetails = $false
|
||||||
|
try {
|
||||||
|
$cache = Import-SurfaceDriverIndexCache -DriversFolder $DriversFolder
|
||||||
|
$cachedDetails = @($cache.DownloadCenterDetails | Where-Object { $_.Link -eq $modelLink } | Select-Object -First 1)
|
||||||
|
if ($cachedDetails.Count -gt 0 -and $cachedDetails[0].Files -and $cachedDetails[0].Files.Count -gt 0) {
|
||||||
|
$useCachedDownloadCenterDetails = $true
|
||||||
|
WriteLog "Surface cache: Using cached Download Center details for $modelName from $modelLink"
|
||||||
|
|
||||||
|
foreach ($downloadFile in @($cachedDetails[0].Files)) {
|
||||||
|
if ($null -eq $downloadFile) { continue }
|
||||||
|
$currentFileName = $downloadFile.Name
|
||||||
|
$fileUrl = $downloadFile.Url
|
||||||
|
if ([string]::IsNullOrWhiteSpace($currentFileName) -or [string]::IsNullOrWhiteSpace($fileUrl)) { continue }
|
||||||
|
|
||||||
|
if ($currentFileName -match "Win10") {
|
||||||
|
$win10Link = $fileUrl
|
||||||
|
$win10FileName = $currentFileName
|
||||||
|
WriteLog "Found Win10 link (cached): $win10FileName"
|
||||||
|
}
|
||||||
|
elseif ($currentFileName -match "Win11") {
|
||||||
|
$win11Link = $fileUrl
|
||||||
|
$win11FileName = $currentFileName
|
||||||
|
WriteLog "Found Win11 link (cached): $win11FileName"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
WriteLog "Surface cache: Failed loading cached Download Center details for '$modelName'. Error: $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Cache miss: download and parse the model's Download Center page (Source C), then backfill the cache
|
||||||
|
if (-not $useCachedDownloadCenterDetails) {
|
||||||
WriteLog "Getting download page content for $modelName from $modelLink"
|
WriteLog "Getting download page content for $modelName from $modelLink"
|
||||||
$OriginalVerbosePreference = $VerbosePreference
|
$OriginalVerbosePreference = $VerbosePreference
|
||||||
$VerbosePreference = 'SilentlyContinue'
|
$VerbosePreference = 'SilentlyContinue'
|
||||||
@@ -205,12 +246,6 @@ function Save-MicrosoftDriversTask {
|
|||||||
$downloadFilePattern = '"name":"([^"]+\.(?:msi|zip))",[^}]*?"url":"(.*?)"'
|
$downloadFilePattern = '"name":"([^"]+\.(?:msi|zip))",[^}]*?"url":"(.*?)"'
|
||||||
$downloadFileMatches = [regex]::Matches($scriptContent, $downloadFilePattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
|
$downloadFileMatches = [regex]::Matches($scriptContent, $downloadFilePattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
|
||||||
|
|
||||||
|
|
||||||
$win10Link = $null
|
|
||||||
$win10FileName = $null
|
|
||||||
$win11Link = $null
|
|
||||||
$win11FileName = $null
|
|
||||||
|
|
||||||
# Iterate through all matches to find potential Win10 and Win11 links
|
# Iterate through all matches to find potential Win10 and Win11 links
|
||||||
foreach ($downloadFile in $downloadFileMatches) {
|
foreach ($downloadFile in $downloadFileMatches) {
|
||||||
$currentFileName = $downloadFile.Groups[1].Value
|
$currentFileName = $downloadFile.Groups[1].Value
|
||||||
@@ -262,6 +297,11 @@ function Save-MicrosoftDriversTask {
|
|||||||
WriteLog "Surface cache: Failed updating Download Center details cache for '$modelName'. Error: $($_.Exception.Message)"
|
WriteLog "Surface cache: Failed updating Download Center details cache for '$modelName'. Error: $($_.Exception.Message)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$useCachedDownloadCenterDetails = $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($useCachedDownloadCenterDetails) {
|
||||||
# Decision logic to select the appropriate download link
|
# Decision logic to select the appropriate download link
|
||||||
$downloadLink = $null
|
$downloadLink = $null
|
||||||
$fileName = $null
|
$fileName = $null
|
||||||
|
|||||||
Reference in New Issue
Block a user