functions/common/Test-UrlExists.ps1
function Test-UrlExists { <# .SYNOPSIS Tests whether a given URL exists and is accessible. .DESCRIPTION Sends a HEAD request to the specified URL to determine if it is reachable (e.g., returns a successful response). Coming soon(TM): If the URL results in an HTTP redirect (3xx), a warning is displayed indicating the redirection target. Returns $true if the request succeeds, otherwise $false. .PARAMETER Url The URL to test for availability. .OUTPUTS [bool] True if the URL is accessible; otherwise, false. .EXAMPLE Test-UrlExists -Url "https://example.com/file.zip" Tests if the specified URL is reachable. Coming soon(TM): Displays a warning if a redirect occurs. .NOTES Uses Invoke-WebRequest with the HEAD method to avoid downloading the full response content. #> param ( [Parameter(Mandatory = $true)] [string]$Url ) try { $response = Invoke-WebRequest -Uri $Url -Method Head -UseBasicParsing -ErrorAction Stop # TODO: detect redirects # if ($redirectUri.TrimEnd('/') -ne $Url.TrimEnd('/')) { # Write-Warning "Url $Url redirects to $redirectUri" # } return $response.BaseResponse.IsSuccessStatusCode } catch { return $false } } |