Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Invoke-WebRequest follow redirect when ran from catch block

Tags:

powershell

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?

like image 382
knurmia Avatar asked Jan 24 '26 20:01

knurmia


1 Answers

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.

like image 69
NitrusCS Avatar answered Jan 27 '26 12:01

NitrusCS