I'm running the code on PowerShell Core v6.1.0 on Mac OS X 10.14.5.
I have a list of websites I'm trying to validate using Invoke-WebRequest. Lets say the list contains the following websites
http://www.google.com
http://www.microsoft.com
http://www.hp.com
https://go.microsoft.com/fwlink/?LinkID=835028 http://samanage.bz
http://samanage.agency
If I use the code below to validate the sites are accessible, everything works fine.
$sites = Get-Content ./Sites.txt
foreach ($site in $sites){
$request = Invoke-Webrequest $site
Write-Host "Status for $site is $($request.StatusDescription)."
}
After checking each website is valid, I wanted to see if the sites are redirected somewhere, so I added try/catch and limited maximum redirections to 0
$sites = Get-Content ./Sites.txt
foreach ($site in $sites){
try{
$request = Invoke-WebRequest $site -MaximumRedirection 0
$siteStatus = $request.StatusDescription
}
catch{
Write-Host "$site is being redirected"
$request = Invoke-WebRequest $site -Method HEAD
# I actually use regex to pull out the destination site
# from the Headers.Link attribute, but left it out for
# simplicity
$siteStatus = $request.Headers.Link
}
Write-Host "Status for $site is $siteStatus."
}
The expectation being that the first request would fail for any website using redirection and get caught in the catch block. Then the request would be rerun without the redirection limit and the redirect destination could be pulled from Headers.Link.
The code works fine for the first four entries, but the bottom two don't follow the redirection correctly and instead result in this error:
Invoke-WebRequest : Response status code does not indicate success: 302 (Found)
I've tried asking Google, but haven't found out what would cause Invoke-WebRequest to give different results when I run them inside the catch block.
Anybody have an idea what is causing this?
There's a new option in PowerShell 7 -SkipHttpErrorCheck which will cause the 302 not to throw an error but still allow you to capture the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With