public/Get-RedirectedURL.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<# RePass Cloud Get-RedirectedURL.ps1 Copyright 2020 RePass Cloud Pty Ltd This product includes software developed at RePass Cloud (https://repasscloud.com/). Version: 2.0.2.5 Last Updated: 2020-08-02 #> function Get-RedirectedUrl { param( [Parameter(Mandatory=$true, Position=0)] [Uri] $Url, [Parameter(Position=1)] [Microsoft.PowerShell.Commands.WebRequestSession] $Session=$null ) $request_url=$Url $retry=$false do { try { $response=Invoke-WebRequest -Method Head -WebSession $Session -Uri $request_url -UseBasicParsing if ($null -ne $response.BaseResponse.ResponseUri) { # PowerShell 5 $result=$response.BaseResponse.ResponseUri.AbsoluteUri } elseif ($null -ne $response.BaseResponse.RequestMessage.RequestUri) { # PowerShell Core $result=$response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri } $retry=$false } catch { if ( ($_.Exception.GetType() -match "HttpResponseException") -and ($_.Exception -match "302") ) { $request_url=$_.Exception.Response.Headers.Location.AbsoluteUri $retry=$true } elseif ( ($_.Exception.GetType() -match "HttpResponseException") -and ($_.Exception -match "403") ) { $result=($request_url).OriginalString $retry=$false } else { throw $_ } } } while($retry) return $result [System.GC]::Collect() } |