Created
August 12, 2022 17:39
-
-
Save Trenly/a23e07526924be7074ef3560195787b4 to your computer and use it in GitHub Desktop.
Get-UrlResponse PowerShellSnippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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