Insert the following line before invoking your PowerShell WebRequest using either Invoke-RestMethod or Invoke-WebRequest. The resolution and solution I’m using to resolve the problem is to allow TLS, TLS 1.1 and TLS 1.2. Looking at the certificate showed it was valid.Īfter a lot of searching I was able to work around the problem using scenarios from ( here and here), however they weren’t ideal. Thinking it was still TLS related I checked out the SSL Certificate for the URI I was making my webrequests against. However, in this situation that wasn’t the fix. This can be done using this PowerShell one-liner: ::SecurityProtocol = ::Tls12 The ambiguous nature of this error did however make me jump to the conclusion that I probably just needed to enforce TLS 1.2. + FullyQualifiedErrorId : WebCmdletWebResponseException,ĭue to PowerShell defaults, it’s not unusual to have issues with TLS.
$postdata = Invoke-WebRequest -Uri $post.URL -Method Get -UserAgent $. Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. Here is the PowerShell Invoke-WebRequest response that returns: The underlying connection was closed: An unexpected error occurred on a send. + FullyQualifiedErrorId : WebCmdletWebResponseException, Invoke-WebRequest + CategoryInfo : InvalidOperation: (:HttpWebRequest), WebException postdata2 = Invoke-RestMethod -Uri $post.URL -Method Get -UserAgent $. Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
Here is the PowerShell Invoke-RestMethod response that returns: The underlying connection was closed: An unexpected error occurred on a send. What should have been just another quick PowerShell script performing a WebRequest to get some data, turned into a debugging session when both the Invoke-RestMethod and Invoke-WebRequest PowerShell commands were returning The underlying connection was closed: An unexpected error occurred on a send.