Windows PowerShell – The underlying connection was closed: An unexpected error occurred on a send.

After changing the default TLS version on a server to TLS version 1.2, I immediately experienced an error with the Windows PowerShell Invoke-WebRequest method.

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

To resolve this issue I used the following line before invoking Invoke-WebRequest:

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

The will allow the use of TLS versions 1.1 and 1.2

Don’t forget to add a semi colon between the new line and your Invoke-WebRequest method if concatenating them.