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.
It is usually better to apply this trick conditionally, for example http://dzimchuk.net/post/Disable-SSL-certificate-validation-during-development
For scripts we should probably check URLs to decide if we want to skip certificate validation.
LikeLike
In my current situation, I was not able to use the solution you propose. But I will keep it for future reference, thanks for sharing.
LikeLike
Merci Alex
LikeLike