Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests powershell powershell

Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests


This worked perfectly for me. The site defaults to TLS 1.0 and apparently PS doesn't work with that. I used this line:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

My PS scripts (so far all I've tested) have worked perfectly.


The answer is do not do this to solve the SSL issue:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

If you do this, your first https request will work (it seems), however subsequent ones will not. Additionaly at that point you need to close out of the Powershell ISE, and reopen it and then try again (without that line).

This is alluded to in a sentence here http://social.technet.microsoft.com/Forums/windowsserver/en-US/79958c6e-4763-4bd7-8b23-2c8dc5457131/sample-code-required-for-invokerestmethod-using-https-and-basic-authorisation?forum=winserverpowershell - "And all subsequent runs produce this error :", but it wasn't clear what the solution to reset was.


I too was plagued by this for a really long time. It even affected Visual Studio as VS loaded my $PROFILE into it's domain when running NuGet restore.

Seeing your comment above made me realize that I had a custom callback script because of one of our vendors shipped a product with an invalid CN in it's ssl cert.

Long story short, I replaced my script delegate with a compiled c# object (removing the script runspace from the equation).

(separate code block for C# highlighting)

using System.Net;using System.Net.Security;using System.Security.Cryptography.X509Certificates;public static class CustomCertificateValidationCallback {    public static void Install()     {        ServicePointManager.ServerCertificateValidationCallback += CustomCertificateValidationCallback.CheckValidationResult;    }    public static bool CheckValidationResult(        object sender,         X509Certificate certificate,         X509Chain chain,         SslPolicyErrors sslPolicyErrors)    {        // please don't do this. do some real validation with explicit exceptions.        return true;    }}

In Powershell:

Add-Type "" # C# Code[CustomCertificateValidationCallback]::Install()