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

Copper Contributor

PowerShell Script connecting to SharePoint online using Connect-PNPOnline

 

as below code sample

 

$credential = Import-Clixml $currfolderPath"\SPOnlineCredential.xml"
$credentials = New-Object System.Management.Automation.PSCredential ($credential.UserName, $credential.Password)

Write-Host "User Crendtial to Run the script" $credential.UserName
$con=Connect-PnPOnline -Url $siteName -Credentials $credentials –ReturnConnection
$ctx=Get-PnPContext

 

gives below error:

 

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, 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 ---
at PnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()

5 Replies

@Mukund2021 

 

If you run the command below, does it allow you to connect?

 

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

 (It allows TLS, TLS 1.1 and TLS 1.2)

@Harm_Veenstra 

 

Dear Harm_Veenstra, 

 

Thank you for your reply, I have used all the possible Security Protocol on defining on Top of the script and also inside the function of my PowerShell script , Please find the attached screen shot for the same but still I am getting same error, Is it some thing I should upgrade PowerShellGet Module but it is also , at latest version 1.0.0.1, Please guide me for this error 

Thank you so much.

 

With Regards

Mukund

Perhaps upgrading to the newer pnp.powershell module is an idea? https://www.sharepointdiary.com/2021/02/how-to-install-pnp-powershell-module-for-sharepoint-online.h... Has the same commands, when running the older just now it said to uninstall the old version (uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force) and use install-module pnp.powershell
Or did you already use pnp.powershell?

@Harm_Veenstra 

 

Yes, I used SharePointPnPPowerShellOnline 3.13.1909.0 in my script .

The same copy of PowerShell script is executing successfully and connection to SharePoint. online website without any issue on other SharePoint farm servers, but one SP2013 farm UAT and PRD servers are giving the issue of TLS 1.2 unsupported protocols, I have attached the screen shot as well , this online SharePoint.com I am trying to connect with my PowerShell script into the Connection function but it gives error of The underlying connection was closed:

 

 

I am not sure If there is any thing I should work be upgrading the script as its is running ok on another SP2016 farm servers UAT and PRD. Please let me know your suggestions and comments.