-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathGet-RedirectedUrl.ps1
More file actions
56 lines (46 loc) · 1.53 KB
/
Get-RedirectedUrl.ps1
File metadata and controls
56 lines (46 loc) · 1.53 KB
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
52
53
54
55
56
<#
.SYNOPSIS
Aquire the url that is being redirected to when using the passed url parameter
.DESCRIPTION
When a website only supplies a url to the latest version, but that url is redirected
to a different url to aquire the actual binary file.
Then this function can be used to simplify that aquiral.
.PARAMETER url
The url to check for redirection
.PARAMETER referer
An optional parameter to use when a website requires the referer header to
be used
.OUTPUTS
The redirected url when one is found, otherwise returns the same url that was passed.
.LINK
https://wormiecorp.github.io/Wormies-AU-Helpers/docs/functions/get-redirectedurl
#>
function Get-RedirectedUrl {
param(
[Parameter(Mandatory = $true)]
[uri]$url,
[uri]$referer,
[Alias('DisableEscape','RawUrl')]
[switch]$NoEscape
)
$req = [System.Net.WebRequest]::CreateDefault($url)
if ($referer) {
$req.Referer = $referer
}
$resp = $req.GetResponse()
if ($resp -and $resp.ResponseUri.OriginalString -ne $url) {
Write-Verbose "Found redirected url '$($resp.ResponseUri)"
if ($NoEscape -or $($resp.ResponseUri.OriginalString) -match '\%\d+' ) {
$result = $resp.ResponseUri.OriginalString
}
else {
$result = [uri]::EscapeUriString($resp.ResponseUri.OriginalString)
}
}
else {
Write-Warning "No redirected url was found, returning given url."
$result = $url
}
$resp.Dispose()
return $result
}