Skip to content

Instantly share code, notes, and snippets.

@Trenly
Created August 12, 2022 17:39
Show Gist options
  • Save Trenly/a23e07526924be7074ef3560195787b4 to your computer and use it in GitHub Desktop.
Save Trenly/a23e07526924be7074ef3560195787b4 to your computer and use it in GitHub Desktop.
Get-UrlResponse PowerShellSnippet
Function Get-UrlResponse {
Param
(
[Parameter(Mandatory = $true, Position = 0)]
[string] $URL
)
try {
$HTTP_Request = [System.Net.WebRequest]::Create($URL)
$HTTP_Request.UserAgent = 'Microsoft-Delivery-Optimization/10.1'
$HTTP_Response = $HTTP_Request.GetResponse()
$ResponseUri = $HTTP_Response.ResponseUri
$AbsoluteUrl = $HTTP_Response.ResponseUri.AbsoluteUri
$HTTP_Status = [int]$HTTP_Response.StatusCode
$ResponseLength = $HTTP_Response.ContentLength
$Headers = @{}; $HTTP_Response.Headers.ForEach({ $Headers[$_] = $Http_Response.Headers[$_] })
} catch {
$HTTP_Status = 404
}
If ($null -eq $HTTP_Response) { $HTTP_Status = 404 }
Else { $HTTP_Response.Close() }
return @{
Url = $URL
ResponseUrl = $AbsoluteUrl
ResponseCode = $HTTP_Status
ContentLength = $ResponseLength
Headers = $Headers
Response = $ResponseUri
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment