#Script: WebExport.ps1 #Source: https://codemonkeysoftware.atlassian.net param ( $SiteCollectionUrl = $(throw "Please specify parameter SiteCollectionUrl"), $exportFolder = "C:\ContentExport", $RootWebLibraries = "PublishingImages,Documents,SiteCollectionDocuments,SiteCollectionImages,Pages", $SubWebLibraries = "Calendar,Documents,Images,Pages", $deleteExportFolder = $true ) if($RootWebLibraries -ne $null) { $RootWebItemsToExport = $RootWebLibraries.Split(',') } if($SubWebLibraries -ne $null) { $SubWebItemsToExport = $SubWebLibraries.Split(',') } #region IO Functions function Get-ScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value Split-Path $Invocation.MyCommand.Path } function Set-Directory($location) { #[Environment]::CurrentDirectory = Get-ScriptDirectory [IO.Directory]::SetCurrentDirectory((Convert-Path($location))) } #endregion #region SharePoint Snappin Setup cls $snapin="Microsoft.SharePoint.PowerShell" if (get-pssnapin $snapin -ea "silentlycontinue") { write-host -f Green "PSsnapin $snapin is loaded" } else { if (get-pssnapin $snapin -registered -ea "silentlycontinue") { write-host -f Green "PSsnapin $snapin is registered" Add-PSSnapin $snapin write-host -f Green "PSsnapin $snapin is loaded" } else { write-host -f Red "PSSnapin $snapin not found" } } #endregion #region Host Feedback Helpers $logBuffer = New-Object System.Text.StringBuilder function WriteToLog($msg, $msg2) { [Void] $logBuffer.AppendLine(($msg + " " + $msg2)) } function WriteErrorToLog($msg, $msg2) { [Void] $logBuffer.AppendLine() [Void] $logBuffer.AppendLine("ERROR:") [Void] $logBuffer.AppendLine(($msg + " " + $msg2)) [Void] $logBuffer.AppendLine() } function WriteSuccess($msg, $msg2) { Write-Host -ForegroundColor Green $msg $msg2 WriteToLog $msg $msg2 } function WriteFailure($msg, $msg2) { Write-Host -ForegroundColor Red "ERROR: " $msg $msg2 WriteErrorToLog ($msg, $msg2) } function WriteInfo($msg, $msg2) { Write-Host " -" $msg $msg2 WriteToLog $msg $msg2 } function WriteWarning($msg, $msg2) { Write-Warning (" -" + $msg + " " + $msg2) } function WriteInfoNoLine($msg, $msg2) { Write-Host " -" $msg $msg2 -NoNewline WriteToLog $msg $msg2 } function Verify-Admin { ## Check if the script is running under elevated administrator privileges $CurrentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $CurrentPrincipal = New-Object System.Security.Principal.WindowsPrincipal($CurrentIdentity) if (!$CurrentPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host -ForegroundColor Red "- This script should be executed under elevated administrative privileges." Write-Host -ForegroundColor Red "- Use ""Run as Administrator"" to execute PowerShell before running this script." #Read-Host "Press [Enter] key to exit" break } } Verify-Admin #endregion function AddNode([xml] $manifest, $parent, $name, $value) { $node = $manifest.CreateElement($name) $node.InnerText = $value return $parent.AppendChild($node) } function AddAttribute([xml] $manifest, $parent, $name, $value) { $node = $manifest.CreateAttribute($name) $node.Value = $value $parent.Attributes.Append($node) | Out-Null } function GetList($web, $listUrl) { $list = $web.Lists | where { $_.Title -match $listUrl } return $list } function ExportWeb($web, $items, $exportFolder, [xml] $manifest, $manifestParent) { Write-Host "Processing Web: " $web.Url foreach($item in $items) { if([string]::IsNullOrEmpty($item)) { continue } $exportFileName = "{0}.{1}.{2}.cmp" -f $exportSetName, $web.Id, $item $exportFileName = $exportFileName.Replace("/","") $exportFullPath = "{0}\{1}" -f $exportFolder, $exportFileName try { $list = GetList $web $item # if($list -eq $null) # { # Write-Host ("No file {1} at {0}" -f $web.Url, $item) # } if($list -ne $null) { if($web.ServerRelativeUrl.EndsWith("/")) { $url = "{0}{1}" -f $web.ServerRelativeUrl, $list.RootFolder } else { $url = "{0}/{1}" -f $web.ServerRelativeUrl, $list.RootFolder } Write-Host "Exporting Item: $url" $web | Export-SPWeb -ItemUrl $url -Path $exportFullPath -HaltOnError -IncludeUserSecurity:$false -IncludeVersions:CurrentVersion -Force if($?) { WriteSuccess "Created content package $exportFileName" $node = AddNode $manifest $manifestParent "ContentFile" $exportFileName AddAttribute $manifest $node "Url" $web.ServerRelativeUrl AddAttribute $manifest $node "Title" $web.Title AddAttribute $manifest $node "WebTemplate" $web.WebTemplate AddAttribute $manifest $node "Configuration" $web.Configuration AddAttribute $manifest $node "Description" $web.Description } } } catch { Write-Warning $_ } } } function Export($siteCol, $exportSetName, $exportFolder, $RootWebItemsToExport, $SubWebItemsToExport) { if(![System.IO.Directory]::Exists($exportFolder)) { [System.IO.Directory]::CreateDirectory($exportFolder) } [Environment]::CurrentDirectory = $exportFolder Set-Directory $exportFolder [xml] $manifest = New-Object xml $rootNode = $manifest.CreateNode([System.Xml.XmlNodeType]::Element, "Content", "") $manifest.AppendChild($rootNode) | Out-Null if($siteCol -eq $null) { throw "Unable to locat site collection $SiteToExportUrl" } foreach($web in $siteCol.AllWebs) { try { if($web.IsRootWeb) { ExportWeb $web $RootWebItemsToExport $exportFolder $manifest $rootNode } else { ExportWeb $web $SubWebItemsToExport $exportFolder $manifest $rootNode } } finally { Invoke-Command -ScriptBlock { $web.Dispose() } -ErrorAction SilentlyContinue } } #Save data $manifestFileName = "{0}\{1}.xml" -f $exportFolder, $exportSetName $manifest.Save($manifestFileName) WriteSuccess "Manifest file written to $manifestFileName" } if($deleteExportFolder) { #Delete existing items remove-item -path "C:\ContentExport\*.*" -Force -ErrorAction:SilentlyContinue } $siteCol = get-spsite -Limit All | where-object {$_.Url -ieq $SiteCollectionUrl} if($siteCol -ne $null) { Export $siteCol $siteCol.RootWeb.Title $exportFolder $RootWebItemsToExport $SubWebItemsToExport remove-item -path "C:\ContentExport\*.log" -Force } else { throw 'Unable to locate site ' + $SiteCollectionUrl }