pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
985 stars 665 forks source link

The underlying connection was closed when running Get-PnPTenantTemplate and Get-PnPProvisioningTemplate #3000

Closed warrenroscoe closed 3 years ago

warrenroscoe commented 3 years ago

Category

Describe the bug

When running the script below dring office hours, I am pretty much always getting the exception below on both the Get-PnPTenantTemplate and Get-PnPProvisioningTemplate commands. I have tried various script tweaks (ServicePointManager settings) from documented fixes on the internet ... they are in the code, but still I cant get either of the commands above to work reliably. Are there any tweaks required to the script or settings somewhere that I need to change that will help with reliability?

Exception is: "Get-PnPProvisioningTemplate : The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."

Steps to reproduce

Run the script below, adding a valid account, password and url.

param ( [switch] $register , [switch] $getPnP , [switch] $applyPnP )

<# cls;.\O365build.ps1 -getPnP

>

<# Remove-Module -Name SharePointPnPPowerShellOnline Import-Module -Name SharePointPnPPowerShellOnline

>

$startDate = Get-Date; Write-Host $("Starting ... $startDate") -BackgroundColor DarkGreen -ForegroundColor Yellow;

[System.Net.ServicePointManager]::Expect100Continue = $false;

[System.Net.ServicePointManager]::DefaultConnectionLimit = 6; [System.Net.ServicePointManager]::MaxServicePointIdleTime = 20000;

try { $username = "" $password = "" $adminSiteUrl = "https://something-admin.sharepoint.com" $sourceSiteUrl = "https://something.sharepoint.com/sites/PnPBuildTest" $destinationSiteUrl = "https://something.sharepoint.com/sites/PnPBuildTarget" $tenentTemplateFile = "C:\TEMP\TenantTemplate-PnPBuildTest.xml" $tenantTemplateConfiguration = "C:\Projects\Modules\O365build-tenanttemplate-config.json" $logFile = "C:\TEMP\pnpTraceOutput.txt"

$provisioningTemplateFile    = "C:\TEMP\ProvisioningTemplate-PnPBuildTest-01-Vanilla.xml"

$encryptedPassword = convertto-securestring -String $password -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encryptedPassword
Write-Host "connecting";Connect-PnPOnline -Url $adminSiteUrl -Credentials $cred; Write-Host (Get-PnPWeb).Url
Set-PnPTraceLog -On -LogFile $logFile -Level Debug -Delimiter ","

if ($register -eq $true) {
    Write-Host "connecting";Connect-PnPOnline -Url $sourceSiteUrl -Credentials $cred; Write-Host (Get-PnPWeb).Url
    Write-Host "registering"
    Register-PnPManagementShellAccess -SiteUrl $sourceSiteUrl
}

# Extract template
if ($getPnP -eq $true) {
    Write-Host "connecting";Connect-PnPOnline -Url $sourceSiteUrl -Credentials $cred; Write-Host (Get-PnPWeb).Url
    #Write-Host "getting PnP tenant template"
    Get-PnPTenantTemplate       -Force       -SiteUrl $sourceSiteUrl      -Out $tenentTemplateFile -Configuration $tenantTemplateConfiguration
    Write-Host "getting PnP provisioning template"
    Get-PnPProvisioningTemplate -Force                              -Out $provisioningTemplateFile -IncludeSiteGroups -IncludeAllClientSidePages -ListsToExtract "Documents"
}

# Apply template
if ($applyPnP -eq $true) {
    Write-Host "connecting";Connect-PnPOnline -Url $destinationSiteUrl -Credentials $cred; Write-Host (Get-PnPWeb).Url
    Write-Host "applying PnP provisioning template"
    Apply-PnPProvisioningTemplate -Path $provisioningTemplateFile -ExcludeHandlers ApplicationLifecycleManagement
}

# Remove the site

} catch { $catch = $_.exception Write-Host "nnEXCEPTION" -BackgroundColor Red -ForegroundColor Yellow Write-Host $catch.GetType().FullName -BackgroundColor Red -ForegroundColor Yellow Write-Host $catch.ScriptStackTrace -BackgroundColor Red -ForegroundColor Yellow Write-Host $catch.Exception.Message -BackgroundColor Red -ForegroundColor Yellow Write-Host $catch.Exception.StackTrace -BackgroundColor Red -ForegroundColor Yellow if ($catch.Exception.InnerException -ne $null) { Write-Host $catch.Exception.InnerException.Message -BackgroundColor Red -ForegroundColor Yellow Write-Host $catch.Exception.InnerException.StackTrace -BackgroundColor Red -ForegroundColor Yellow } }

$endDate = Get-Date; Write-Host $("Finishing ... $endDate") -BackgroundColor DarkGreen -ForegroundColor Yellow; Write-Host $("Duration ... {0:hh\:mm\:ss.fff}" -f $(New-TimeSpan –Start $startDate –End $endDate)) -BackgroundColor DarkGreen -ForegroundColor Yellow;

Expected behavior

No exception

Environment details (development & target environment)

$psversiontable
Name Value


PSVersion 5.1.18362.1171 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.18362.1171 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

Additional context

The full exception is:

"powershell.exe",Error,0,"2020-11-16 18:33:39.7688 [OfficeDevPnP.Core] [0] [Error] ExecuteQuery threw following exception: System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> 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._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.SPWebRequestExecutor.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientRequest.d6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientRequest.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientRuntimeContext.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.SharePoint.Client.ClientContext.d4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.SharePoint.Client.ClientContextExtensions.d7.MoveNext() . 0ms ",,,,,"2020-11-16T18:33:39.7688523Z",, "powershell.exe",Information,0,"2020-11-16 18:33:39.7728 [Site Security] [14] [Debug] Code execution scope ended 78305ms 67633e81-8917-45f9-af01-aa7779e7ef0d",,,,,"2020-11-16T18:33:39.7728458Z",, "powershell.exe",Information,0,"2020-11-16 18:33:39.7728 [Extraction] [14] [Debug] Code execution scope ended 801209ms 67633e81-8917-45f9-af01-aa7779e7ef0d",,,,,"2020-11-16T18:33:39.7728458Z",,

Thanks for your contribution! Sharing is caring.

ghost commented 3 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

warrenroscoe commented 3 years ago

Hi, I have managed to circumvent this issue (for now). I checked my internet speed, and upload speed was poor. I followed advice in https://answers.microsoft.com/en-us/windows/forum/windows_10-networking/slow-internet-speed-on-windows-10-1809/022c16ef-3c69-4472-b7e9-d0b7466916fc#:~:text=Slower%20internet%20speed%20after%20Windows,for%20the%20Windows%2010%20update.&text=Check%20for%20Receive%20Window%20Auto,will%20need%20to%20disable%20it. and this managed to fix the issue.

warrenroscoe commented 3 years ago

Therefore, closing the issue. Happy Xmas!