PowerShell Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send

As I’m writing more PowerShell (what I really mean is googling PowerShell or asking Steven @StevenMurawski), I ran into this weird thing. I was trying to hit an Azure Function that I wrote using Invoke-RestMethod and I kept getting the error:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send

WTF????? After googling a bunch, turns out this is a TLS issue. PowerShell defaults to TLS1, I needed mine to be at TLS1.2. More googling gave the the fix. Add this before you make your Invoke-RestMethod call

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

I’m sure i’m gonna run into this again so sticking this in my blog so I’ll find it quickly Smile

Leave a Reply

Your email address will not be published. Required fields are marked *