This blog, like any journal, tracks my own personal growth. This means I get to revisit and rehash old posts a lot.   In this case, I’m adding the 302 Redirect check from a few years back to the ServicePointManager callback to accept bad SSL certs into a copy-pastable function to return the 302 Redirect data for a given URL.

Test-Redirect https://gmail.com

Url                                                         302
---                                                         ---
https://gmail.com                                           https://mail.google.com/mail/

 

function Test-Redirect {
     param ( [string[]]$Url = $null );
     begin { 
         [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; 
     }
     process {
         foreach ($myUrl in $Url) {
             $location = $null;
             $webRequest = [System.Net.WebRequest]::Create($myUrl);
             $webRequest.AllowAutoRedirect = $false;
             $true | Select-Object -Property @{ 
                 n = 'Url'; e = { $myUrl; } 
             }, @{ 
                 n = '302'; e = { 
                     try {
                         $response = $webRequest.GetResponse();
                     } 
                     catch {
                         Write-Warning "Unable to get response for -url '$myUrl'";
                     }
                     if ($response) {
                         $response.Headers.Get('Location');
                     }
                 }
             }
         }
     }
}