Last active
May 13, 2022 03:48
-
-
Save crshnbrn66/7ebb2dfd282176f607d96827b78b949f to your computer and use it in GitHub Desktop.
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-RedirectedUrl | |
{ | |
param($url) | |
function Get-RedirectPath | |
{ | |
param($html) | |
$h = convertfrom-html $html | |
if((($h.all.tags('Title')) | select-Object -expandProperty text).toString() -eq "Object moved") | |
{ | |
($H.all.tags('a')) | select-object -first 1 -Expandproperty pathname | |
} | |
elseif((($h.all.tags('Title')) | select-Object -expandProperty text).toString() -eq "Document moved") | |
{ | |
($H.all.tags('a')) | select-object -first 1 -Expandproperty pathname | |
} | |
else | |
{$null} | |
} | |
Function Get-headers | |
{ | |
param([System.Net.HttpWebResponse]$HttpWebResponse) | |
$headerHash = @{} | |
foreach($header in $HttpWebResponse.headers) | |
{ | |
$headerhash += @{$header = $response.GetresponseHeader($header)} | |
} | |
$headerHash | |
} | |
function ConvertFrom-html | |
{ | |
param([string]$html) | |
$h = new-object -com "HTMLFile" | |
$H.IHTMLDocument2_write($html) | |
$h | |
} | |
$request = [System.Net.WebRequest]::Create($url) | |
$response=$request.GetResponse() | |
$stream = $response.GetREsponseStream() | |
$streamREader = [System.IO.StreamREader]::new($stream) | |
$html = $streamREader.ReadToEnd() | |
if($response.Statuscode.toString() -eq "Found") | |
{ | |
$headers = get-headers $response | |
$headers += @{html = Convertfrom-html $html} | |
$headers +=@{datetime = Get-Date} | |
[pscustomobject]$headers | |
} | |
elseif($response.Statuscode.toString() -eq "MovedPermanently") | |
{ | |
$headers = get-headers $response | |
$headers +=@{redirect = $redirect} | |
$headers += @{html = Convertfrom-html $html} | |
$headers +=@{datetime = Get-Date} | |
[pscustomobject]$headers | |
} | |
elseif($response.Statuscode.toString() -eq "Redirect") | |
{ | |
$headers = get-headers $response | |
$headers +=@{redirect = $redirect} | |
$headers += @{html = Convertfrom-html $html} | |
$headers +=@{datetime = Get-Date} | |
[pscustomobject]$headers | |
} | |
elseif($response.Statuscode.toString() -eq "OK") | |
{ | |
$headers = get-headers $response | |
$headers +=@{redirect = $redirect} | |
$headers += @{html = Convertfrom-html $html} | |
$headers +=@{datetime = Get-Date} | |
[pscustomobject]$headers | |
} | |
if($streamREader) | |
{$streamREader.close()} | |
if($response) | |
{$response.close()} | |
} |
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-redirectedUrls | |
{ | |
Param($url) | |
$urlcheckobject=@{} | |
$Uri = [uri]$url | |
$url2check = $url | |
do{ | |
$value = get-redirectedUrl -url $url2check | |
if($value.redirect) | |
{ | |
$Url2check = "$(uri.scheme)://$($uri.Host)/$($value.redirect)" | |
$value |add-member -MemberType NoteProperty -name url -value $Url2check | |
$urlcheckobject += $value | |
} | |
else | |
{ | |
$url2check = $Null | |
} | |
} | |
until ($url2check -eq $null) | |
$urlcheckobject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment