Skip to content

Instantly share code, notes, and snippets.

@SpeedmaxX
Created January 25, 2019 06:13
Show Gist options
  • Save SpeedmaxX/dbebb9261a3461375feeb6f5ff8e87a7 to your computer and use it in GitHub Desktop.
Save SpeedmaxX/dbebb9261a3461375feeb6f5ff8e87a7 to your computer and use it in GitHub Desktop.
Copy O365 SiteCollection
$SourceWebUrl = "https://sourcesite"
$TargetWebUrl = "https://targetsite"
Set-PnPTraceLog -On -Level Debug
if ($SourceWebUrl.EndsWith("/")) {
$SourceWebUrl = $SourceWebUrl.Substring(0, $SourceWebUrl.Length - 1)
}
if ($TargetWebUrl.EndsWith("/")) {
$TargetWebUrl = $TargetWebUrl.Substring(0, $TargetWebUrl.Length - 1)
}
$cred = Get-Credential -Message "Bitte geben Sie die Anmeldedaten eines WebSiteSammlungsadministratorens beider Systeme ein:"
if ([String]::IsNullOrEmpty($cred.UserName) -or $cred.Password.Length -le 0) {
Write-Error "invalid Credentials!"
return
}
try {
$sourceCon = Connect-PnPOnline -Url $SourceWebUrl -Credentials $cred -ReturnConnection
$targetCon = Connect-PnPOnline -Url $TargetWebUrl -Credentials $cred -ReturnConnection
}
catch {
Write-Error "it was not possible to make a connection: $($_.Exception.Message)"
#region Close connections
if ($null -ne $sourceCon) {
Disconnect-PnPOnline -Connection $sourceCon
$sourceCon = $null
}
if ($null -ne $targetCon) {
Disconnect-PnPOnline -Connection $targetCon
$targetCon = $null
}
#endregion
return
}
#region available Handlers
# All = AuditSettings | ComposedLook | CustomActions | ExtensibilityProviders | Features | Fields | Files | Lists | Pages | Publishing | RegionalSettings | SearchSettings | SitePolicy | SupportedUILanguages | TermGroups | Workflows | SiteSecurity | ContentTypes | PropertyBagEntries | PageContents | WebSettings | Navigation | ImageRenditions | ApplicationLifecycleManagement | Tenant | WebApiPermissions
#endregion
try {
if ($null -ne $sourceCon) {
write-host "SourceSite: $($sourceCon.Url)"
Get-PnPProvisioningTemplate -Connection $sourceCon -Out YourSiteTemplate.xml -ContentTypeGroups "ecspand - Teamspace,Projektinformationen" -ExcludeHandlers ComposedLook,PropertyBagEntries,SiteSecurity,TermGroups,AuditSettings,Features,Navigation,SupportedUILanguages,RegionalSettings,WebSettings,Lists,Fields
}
$template = Read-PnPProvisioningTemplate -Path .\YourSiteTemplate.xml
#region Lists & WebSiteSites
for ($i = $template.SiteFields.Count - 1; $i -ge 0; $i--) {
$field = $template.SiteFields[$i]
if ($field.SchemaXml.Contains("Name=`"_DisplayName`"") -or $field.SchemaXml.Contains("ReadOnly=`"TRUE`"") -or $field.SchemaXml.Contains("Hidden=`"TRUE`"") -or $field.SchemaXml.Contains("Sealed=`"TRUE`"") -or $field.SchemaXml.Contains("Group=`"_Hidden`"")) {
$template.SiteFields.RemoveAt($i)
}
}
#endregion
#region Lists & WebSiteSites
for ($i = $template.Lists.Count - 1; $i -ge 0; $i--) {
$listInstance = $template.Lists[$i]
write-host $listInstance.Title
if ($listInstance.Url -eq "Style Library") {
$template.Lists.RemoveAt($i)
}
else {
for ($j = $listInstance.Fields.Count - 1; $j -ge 0; $j--) {
$field = $listInstance.Fields[$j]
if ($field.SchemaXml.Contains("Name=`"_DisplayName`"") -or $field.SchemaXml.Contains("Sealed=`"TRUE`"") -or $field.SchemaXml.Contains("Group=`"_Hidden`"")) {
$listInstance.Fields.RemoveAt($j)
}
}
}
}
#endregion
Save-PnPProvisioningTemplate -InputInstance $template -Out .\YourSiteTemplate.xml -Force
}
catch {
Write-Error "Error modifying template: $($_.Exception.Message)"
#region Close connections
if ($null -ne $sourceCon) {
Disconnect-PnPOnline -Connection $sourceCon
}
if ($null -ne $targetCon) {
Disconnect-PnPOnline -Connection $targetCon
}
#endregion
return
}
try {
if ($null -ne $targetCon) {
write-host "TargetSite: $($targetCon.Url)"
Apply-PnPProvisioningTemplate -Path .\YourSiteTemplate.xml -Connection $targetCon -ExcludeHandlers SiteSecurity,TermGroups,Navigation -ClearNavigation
}
}
catch {
Write-Error "Error applying template: $($_.Exception.Message)"
#region Close connections
if ($null -ne $sourceCon) {
Disconnect-PnPOnline -Connection $sourceCon
}
if ($null -ne $targetCon) {
Disconnect-PnPOnline -Connection $targetCon
}
#endregion
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment