Azure WebJob Invoke-RestMethod Underlying Connection Closed Resolution

I recently ran into a situation where a Powershell script that contained an Invoke-RestMethod worked fine locally on my VM, but would fail as an Azure WebJob with the following error:

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

This is because PowerShell currently uses TLS 1.0 by default, which was replaced with it's more secure counterpart TLS 1.2.

To resolve this issue, you need to add the following command to the top of your PowerShell script - to force PowerShell to use TLS 1.2 instead:

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

No Comments