Friday, 21 October 2016

Migrating sites from one location to another location using PowerShell

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# add SharePoint snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell –ea SilentlyContinue

Start-SPAssignment -Global

$starttime = Get-Date

#Creating new site object
$siteurl = Read-Host "Enter the name of your site and press enter"
$site = New-Object Microsoft.SharePoint.SPSite($siteurl)

#Assigning all webs (sites) to $webs
$webs = $site.Allwebs

$systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates",
                              "Images", "List Template Gallery", "Master Page Gallery", "Pages",
                               "Reporting Templates", "Site Assets", "Site Collection Documents",
                               "Site Collection Images", "Site Pages", "Solution Gallery",
                               "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub")

Write-Host  "Total number of webs that will be traversed: " $webs.count

$DocLibsCount = 0
$DocLibwItems = 0
$totalitems = 0
$subfolderitems = 0

foreach($web in $webs)
    {
        $listcoll = $web.lists
     
        foreach($list in $listcoll)
        {
             if($list -eq $null)
             {
                Write-Host
             }
             else
             {
                $base = $list.GetType()
                if($base.name -eq "SPDocumentLibrary")
                {
                    if ($systemlibs -contains $list)
                    { continue}
                    else
                        {
                          $DocLibsCount += 1  
                          $items = $list.items
                       
                          if($items -ne "0")
                          {
                           $DocLibwItems += 1
                           Write-Host "Processing ItemCount for DobLib " $DocLibsCount -ForegroundColor Red
                           $totalitems  += $items.count
                 
                           $name = $list.Title
                           $folders  = $web.GetFolder($name).SubFolders
                           for($etr = 0;$etr -lt $folders.count; $etr++)
                            {
                             if($folders[$etr].Name -ne "Forms")
                               {
                                 Write-Host "Processing SubFolder ItemCount" -ForegroundColor Red
                                 $tempcount = $folders[$etr].ItemCount
                                 $subfolderitems += $tempcount
                                 }
                            }
                           }
                          }
                   }
                 }
              }
          }

Write-Host
Write-Host
Write-Host "Total # of Document Libraries: " $DocLibsCount -ForegroundColor Green
Write-Host "Total # of Document Libraries that contain items: " $DocLibwItems -ForegroundColor Green
Write-Host "Total # of items: " $totalitems -ForegroundColor Green
Write-Host "Total # of items in DocLib\Subfolders: " $subfolderitems -ForegroundColor Green
$finishtime = Get-Date
Write-Host
Write-Host “Script Duration” –ForegroundColor Yellow
Write-Host “Started:   “ $starttime –ForegroundColor Yellow
Write-Host “Finished: “ $finishtime –ForegroundColor Yellow

#create export folder

$exportfolder = "C:\Site exports"
$exportfile = "\site_export.cmp"
$exportsite = "http://Sourcesite/abc"
$exportlocation = $exportfolder+$exportfile
$importlocation = "http://destination/abc"

$null = New-Item $exportfolder -type directory
#export site
Export-SPWeb $exportsite –Path $exportlocation -IncludeUserSecurity -IncludeVersions All
Write-host "$exportsite has been exported to $exportlocation"
#create new site ready for import
Write-host "$importlocation created ready for import"
#import site
Import-SPWeb $importlocation –Path $exportlocation -IncludeUserSecurity –UpdateVersions Overwrite
Write-host "$exportsite has been imported to $importlocation" -foregroundcolor "Green"


#Creating new site object
$starttime = Get-Date

$siteurl = Read-Host "Enter the name of your site and press enter"
$site = New-Object Microsoft.SharePoint.SPSite($siteurl)

#Assigning all webs (sites) to $webs
$webs = $site.Allwebs

$systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates",
                              "Images", "List Template Gallery", "Master Page Gallery", "Pages",
                               "Reporting Templates", "Site Assets", "Site Collection Documents",
                               "Site Collection Images", "Site Pages", "Solution Gallery",
                               "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub")

Write-Host  "Total number of webs that will be traversed: " $webs.count

$DocLibsCount = 0
$DocLibwItems = 0
$totalitems = 0
$subfolderitems = 0

foreach($web in $webs)
    {
        $listcoll = $web.lists
     
        foreach($list in $listcoll)
        {
             if($list -eq $null)
             {
                Write-Host
             }
             else
             {
                $base = $list.GetType()
                if($base.name -eq "SPDocumentLibrary")
                {
                    if ($systemlibs -contains $list)
                    { continue}
                    else
                        {
                          $DocLibsCount += 1  
                          $items = $list.items
                       
                          if($items -ne "0")
                          {
                           $DocLibwItems += 1
                           Write-Host "Processing ItemCount for DobLib " $DocLibsCount -ForegroundColor Red
                           $totalitems  += $items.count
                 
                           $name = $list.Title
                           $folders  = $web.GetFolder($name).SubFolders
                           for($etr = 0;$etr -lt $folders.count; $etr++)
                            {
                             if($folders[$etr].Name -ne "Forms")
                               {
                                 Write-Host "Processing SubFolder ItemCount" -ForegroundColor Red
                                 $tempcount = $folders[$etr].ItemCount
                                 $subfolderitems += $tempcount
                                 }
                            }
                           }
                          }
                   }
                 }
              }
          }

Write-Host
Write-Host
Write-Host "Total # of Document Libraries: " $DocLibsCount -ForegroundColor Green
Write-Host "Total # of Document Libraries that contain items: " $DocLibwItems -ForegroundColor Green
Write-Host "Total # of items: " $totalitems -ForegroundColor Green
Write-Host "Total # of items in DocLib\Subfolders: " $subfolderitems -ForegroundColor Green
$finishtime = Get-Date
Write-Host
Write-Host “Script Duration” –ForegroundColor Yellow
Write-Host “Started:   “ $starttime –ForegroundColor Yellow
Write-Host “Finished: “ $finishtime –ForegroundColor Yellow

Stop-SPAssignment -Global

No comments:

Post a Comment