Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Last active December 14, 2017 22:11
Show Gist options
  • Save motowilliams/5d892b318d5d07b87f0cd394bfbd3fa3 to your computer and use it in GitHub Desktop.
Save motowilliams/5d892b318d5d07b87f0cd394bfbd3fa3 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
param(
[string][Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]$uri,
$TimeoutSec = 5
)
process {
if ($uri.StartsWith([System.Uri]::UriSchemeHttp) -eq $false) {
$uri = [System.Uri]::UriSchemeHttp + [System.Uri]::SchemeDelimiter + $uri
}
[System.Uri]$uri = [System.Uri]$uri
$expected = [System.Uri]::UriSchemeHttps + [System.Uri]::SchemeDelimiter + $uri.Authority + $uri.PathAndQuery
if ($uri.Scheme -eq [System.Uri]::UriSchemeHttps) {
Write-Host -ForegroundColor DarkYellow Selected uri is already on ([System.Uri]::UriSchemeHttps)
return
}
try {
$response = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -TimeoutSec $TimeoutSec -ErrorAction Ignore
}
catch {
Write-Host -ForegroundColor Red $_.Exception.Message
return
}
if ($response.Headers.Location -eq $null) {
Write-Host -ForegroundColor Red "Response from $uri did not return a location http header"
return
}
if ($expected -eq $response.Headers.Location) {
Write-Host -ForegroundColor Green $uri redirects ($response.StatusCode) to $response.Headers.Location
}
else {
Write-Host -ForegroundColor Red $uri redirects to $response.Headers.Location expected $expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment