Improves ADK FWLink resolution for robustness

Refactors the ADK URL retrieval logic to let `Invoke-WebRequest` handle the forward link redirection directly. This approach is more reliable than manually parsing the redirect response.

Adds a try/catch block to provide better error handling and logging during the URL resolution process.
This commit is contained in:
rbalsleyMSFT
2025-08-01 12:36:45 -07:00
parent 41b65a76c1
commit eae07fcad0
+16 -7
View File
@@ -1648,21 +1648,30 @@ function Get-ADKURL {
return return
} }
# Retrieve headers of the FWlink URL # Let Invoke-WebRequest handle the redirect and get the final URL.
try {
$OriginalVerbosePreference = $VerbosePreference $OriginalVerbosePreference = $VerbosePreference
$VerbosePreference = 'SilentlyContinue' $VerbosePreference = 'SilentlyContinue'
$FWLinkRequest = Invoke-WebRequest -Uri $ADKFWLink -Method Head -MaximumRedirection 0 -ErrorAction SilentlyContinue # Allow one redirection to get the final URL from the fwlink
$response = Invoke-WebRequest -Uri $ADKFWLink -Method Head -MaximumRedirection 1 -Headers $Headers -UserAgent $UserAgent
$VerbosePreference = $OriginalVerbosePreference $VerbosePreference = $OriginalVerbosePreference
if ($FWLinkRequest.StatusCode -ne 302) { # The final URL after redirection is in the ResponseUri property of the BaseResponse's RequestMessage.
WriteLog "Failed to retrieve ADK download URL. Unexpected status code: $($FWLinkRequest.StatusCode)" $ADKUrl = $response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri
return
if ($null -eq $ADKUrl) {
WriteLog "Could not determine final ADK download URL after redirection."
return $null
} }
# Get the ADK link redirected to by the FWlink WriteLog "Resolved ADK download URL to: $ADKUrl"
$ADKUrl = $FWLinkRequest.Headers.Location
return $ADKUrl return $ADKUrl
} }
catch {
WriteLog "An error occurred while resolving the ADK FWLink: $($_.Exception.Message)"
throw
}
}
catch { catch {
WriteLog $_ WriteLog $_
Write-Error "Error occurred while retrieving ADK download URL" Write-Error "Error occurred while retrieving ADK download URL"