43

I have a rest endpoint that doesn't perform an authentication check. I can run a simple curl commmand from Linux:

curl -k https://application/api/about

This responds.

However if try the following on PowerShell it fails:

Invoke-RestMethod https://application/api/about

Then I get:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-RestMethod $Application
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Could some one please tell me how I can get around this problem ?

EDIT:

Trying with Invoke-WebRequest:

Invoke-WebRequest -Uri "https://application/api/about"

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. At line:1 char:1 + Invoke-WebRequest -Uri "https://application/api/a ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

3
  • Could you just use Invoke-WebRequest instead? Jan 27, 2017 at 15:19
  • I wish I could... Same error (I'll post result in Edit)
    – dross
    Jan 27, 2017 at 15:28
  • I see you're using https, do you have certs setup correctly? Try running this first, "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}", then try the Invoke-webRequest again Jan 27, 2017 at 17:32

3 Answers 3

90

Using:

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

Taken from Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests.

7
  • Thank you! This was exactly what I needed Aug 10, 2018 at 23:47
  • This is gold! Worked like a charm.
    – qJake
    Oct 23, 2018 at 14:48
  • unbelievable 15K views... 48 up votes. thanks for the info. Feb 6, 2019 at 15:51
  • How did you know to set this value? I just want to know how we were all supposed to figure this out
    – aaaaaa
    Apr 9, 2019 at 22:04
  • @aaaaaa - we figured it out by searching StackOverflow :) and found this answer Apr 19, 2019 at 11:12
22

in my case the TLS trick did not work, this seems to be a bug in powershell. you need to add the callback using .net code instead of a scriptblock.

#C# class to create callback
$code = @"
public class SSLHandler
{
    public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
    {

        return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
    }

}
"@

#compile the class
Add-Type -TypeDefinition $code

#disable checks using new class
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()
#do the request
try
{
    invoke-WebRequest -Uri myurl -UseBasicParsing
} catch {
    # do something
} finally {
   #enable checks again
   [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
}
4
  • 3
    This worked for me while setting [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} and [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy didn't. Mar 10, 2019 at 17:24
  • @justin I do not understand why this worked, but it did. Is it a powershell version issue? Jun 5, 2019 at 16:48
  • @AndyArismendi using {$true} does work, but you can get yourself into trouble if there are too many calls to https. that is why you need a compiled class.
    – Justin
    Jun 13, 2019 at 2:13
  • @MonaLisaOverdrive as far as i can tell it is a powershell issue, basically it keeps reloading the powershell runspace and eventually reaches a point where it can't.
    – Justin
    Jun 13, 2019 at 2:13
2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

Works in Windows server 2016

Major Minor Build Revision


5 1 17763 1007

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.