2

I have a similar question realted to this post

An Unexpected error occured on a send

My issue is slightly different where I'm actually getting a response back from the server on a windows server 2012 R2, but my local machine I'm getting

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

I understand that the server is ending the connection or the timeout is too low is the prescribed reason via Microsoft. This is the same script on 2 different machines one working, one not. My local machine is what is the error-thrower.

Obviously this is not a direct issue with my script rather my localhost so I'm hoping someone could provide some insight as to why this works on a server host and not my laptop on the same domain?

If I use the try/catch on my local, all I see is this error. If I remove the try catch, I get the full response

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\blah some directory Examples\blah\myscript.ps1:82 char:17
+     $response = Invoke-WebRequest -Headers $headers -Method GET -ContentType "ap ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

id: 67kjadsfj5029838k3ka70387fe400
state: active
[I get a response]

So I'm actually getting a response back from the host with the requested data so I have to take the invokation out of the try/catch on my local to get it. Obviously not ideal, I'd prefer to keep the script in-tact

Ultimately, I am hoping if someone could provide some insight as to why powershell on my local machine is throwing this error and yet giving me the response, how do I suppress or fix it? If I could get the why and how that would be great but I'd settle for how at this point.

Autonomic
  • 150
  • 4
  • 15
  • 1
    Can you add the parameters you are passing to Invoke-WebRequest? That would help in understanding your issue. Since you are dealing with localhost it could be something related the BackConnectionHostNames registry key. – Goyuix Jun 20 '17 at 16:10
  • I am passing in 4 strings, because my script has required params set the at execution time I fill in the prompts for input. Otherwise the request is a GET method Im not passing any data to the host except in the header. – Autonomic Jun 20 '17 at 17:19

2 Answers2

1

how do I suppress or fix it?

If you're concerned with suppressing error messages, you can utilize the -ea 'Silently Continue' / -wa 'Silently Continue' args on the WGET cmdlet

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
0

So, my programs often crash, like VS Code, which I was using. Even in VS Code while executing PowerShell, you have to right-click > run as administrator. Once I did that, the errors went away. But great answer TheIncorrigble1, I kept your example for future reference

Autonomic
  • 150
  • 4
  • 15