0

The following error message is encountered when executing Invoke-WebRequest. It is found on the Internet that the following command needs to be added before the command, but the error is still the same.

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

At present, it is known that the server to be connected has enable TLS1.2 (only), the service uses https, the firewall is connected, and the telnet servicename 443 is connected, and the server that executes PowerShell has enable TLS1.0, 1.1, 1.2, and PowerShell uses v4. 0. As a comparison, I use my own computer and execute Invoke-WebRequest successfully, but PowerShell is v5.1. I wonder if it is a PowerShell version problem? Please help, thank you.

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:15
+ $WebConnect = Invoke-WebRequest -URI $strHTTPS -Credential $credential
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest)     [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId :     WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Eric46900
  • 3
  • 1
  • 1
  • 4
  • This might help: [Powershell 4 too old for tls 1.2? - Stack Overflow](https://stackoverflow.com/a/60622983/3165737). – DocZerø Mar 16 '22 at 12:02
  • Thank you. This way I confirm that my PowerShell does not support TLS1.2 transport. I also asked the other party to enable TLS1.0 and 1.1 of the VM, but the same error message is still there. I also found that the error message (about TLS1.2) in the URL you provided did not appear. I guess other problems caused this error? – Eric46900 Mar 17 '22 at 01:53

2 Answers2

0

In my case, added a callback following to Unexpected error occurred running a simple unauthorized Rest query and Invoke-RestMethod works even though Invoke-WebRequest doesn't work.

Kenta
  • 1
0

I include an answer that solved our problem to this exact error message (and might save someone lots of time): In our case the problem was due to a forgotten Hosts file entry (from testing phase) that was over-riding the DNS mapping. When the website tried invoking a WebRequest to itself, the request was directed to the old (test phase) IP address, and therefore failing. This was particularly frustrating since all other sites on the same server were responding to Invoke-WebRequest just fine.

JayRO-GreyBeard
  • 149
  • 2
  • 7