From eae07fcad027ec4866b7fcd0b9b08699b210921e Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Fri, 1 Aug 2025 12:36:45 -0700 Subject: [PATCH] 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. --- FFUDevelopment/BuildFFUVM.ps1 | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index e8311d0..beb81f4 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1648,20 +1648,29 @@ function Get-ADKURL { return } - # Retrieve headers of the FWlink URL - $OriginalVerbosePreference = $VerbosePreference - $VerbosePreference = 'SilentlyContinue' - $FWLinkRequest = Invoke-WebRequest -Uri $ADKFWLink -Method Head -MaximumRedirection 0 -ErrorAction SilentlyContinue - $VerbosePreference = $OriginalVerbosePreference - - if ($FWLinkRequest.StatusCode -ne 302) { - WriteLog "Failed to retrieve ADK download URL. Unexpected status code: $($FWLinkRequest.StatusCode)" - return + # Let Invoke-WebRequest handle the redirect and get the final URL. + try { + $OriginalVerbosePreference = $VerbosePreference + $VerbosePreference = '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 + + # The final URL after redirection is in the ResponseUri property of the BaseResponse's RequestMessage. + $ADKUrl = $response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri + + if ($null -eq $ADKUrl) { + WriteLog "Could not determine final ADK download URL after redirection." + return $null + } + + WriteLog "Resolved ADK download URL to: $ADKUrl" + return $ADKUrl + } + catch { + WriteLog "An error occurred while resolving the ADK FWLink: $($_.Exception.Message)" + throw } - - # Get the ADK link redirected to by the FWlink - $ADKUrl = $FWLinkRequest.Headers.Location - return $ADKUrl } catch { WriteLog $_