pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets

Home Page:https://pnp.github.io/powershell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

warrenroscoe opened this issue · comments

Category

  • Bug

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]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
[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)

  • SDK version: 3.26.2010.0 (for SharePointPnPPowerShellOnline)
  • OS: Windows 10
  • SDK used in: SharePointPnPPowerShellOnline
  • Framework:
    Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where { $_.PSChildName -Match '^(?!S)\p{L}'} | Select PSChildName, version
    PSChildName Version

Client 4.8.03752
Full 4.8.03752
Client 4.0.0.0

  • Browser(s):
  • Tooling:
  • Additional details:

$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.d__0.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.d__6.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.d__0.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.d__0.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.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.SharePoint.Client.ClientContextExtensions.d__7.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.

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

Therefore, closing the issue. Happy Xmas!