The underlying connection was closed

For the past weeks I’ve used PowerShell to test REST APIs exposed over HTTPS. Everything was wonderful until I had to execute my script against a local instance of the service. My machine is not setup with proper certificates and PowerShell doesn’t see to like that. When ever I execute an Invoke-RestMethod command I get the following error message:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

Looking around I was fortunate to find this one-liner that fixes everything!

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

By turning off certificate validation you are disabling security. Beware that this one-liner is a patch, and should not be used for anything else than development purposes.

4 responses to #PowerShell Invoke-RestMethod – the underlying connection was closed


    It is usually better to apply this trick conditionally, for example

    For scripts we should probably check URLs to decide if we want to skip certificate validation.


Trackbacks and Pingbacks:

  1. Reading Notes 2014-09-15 | Matricis - September 15, 2014

    […] #PowerShell Invoke-RestMethod – the underlying connection was closed – Useful tips to save some headache while developing. […]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.