PowerCLI Connection Error: The underlying connection was closed: An unexpected error occurred on a send

PowerCLI is a very flexible tool for vSphere automation. One morning, I connect to vSphere environment & found this error. See Fig-1.

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

Fig-1: Error connecting to vCenter

I can ping to vCenter server & can connect to vSphere Web Client. So, I checked for the $Error[0] for exception, it is likely to be a certificate error. But, when I checked the certificate by vSphere Web Client, it’s not going to be expired soon.

PowerCLI C:> $error[0] | fl * -Force
PSMessageDetails      :
Exception             : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViError: 7/22/2017 12:10:08 AM    Connect-VIServer The underlying connection was closed: An unexpected error occurred on a send.     —> System.Net.WebException: The underlying connection was

closed: An unexpected error occurred on a send. —> System.IO.IOException: The handshake failed due to an unexpected packet format.
                           at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
                           at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
                           at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
                           at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
                           at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
                           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state,

Boolean preserveSyncCtx)
                           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
                        preserveSyncCtx)
                           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
                           at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
                           at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
                           at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
                           at System.Net.ConnectStream.WriteHeaders(Boolean async)
                           — End of inner exception stack trace —

So, I check the PowerCLI Connection Settings by the following command, See Fig-2.
Get-PowerCLIConfiguration | fl *

Fig-2 Get PowerCLI Connection Configuration

Now, I found that the connection is using the system proxy (marked in Fig-2). So, I check the system proxy settings.
Control Panel > Internet Options > Connections > LAN Settings > Proxy server  and it is using 127.0.0.1 with 6289 port. See Fig-3.

Fig-3: The proxy is used in Internet Options

So, the solution is we can disable the proxy or change the PowerCLI settings to use NoProxy and I’ll go with the latter. Use the following command. See Fig-4.
Set-PowerCLIConfiguration -Scope User -ProxyPolicy NoProxy

Fig-4: Change PowerCLI configuration to use No Proxy

**Note: the “-Scope” parameter has the following options:
Session – changes are maintained for current opened PowerCLI session only
User – changes are permanent for current user only
AllUsers – changes are permanent for all users

Now, we can connect to vCenter successfully. See Fig-5

Fig-5: Connection is ok now

This entry was posted in PowerCLI, VMware. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *