Tuesday 4 October 2011

How to export and import a site using powershell script in sharepoint 2007 and sharepoint 2010

Hi,

I recently published an article about exporting and importing a site using sharepoint object model. Someone recently asked me for doing the same via powershell scripts. Hence I have done that in this article. This scripts first exports a site and then ask for the decision to go for import or not. If you type "y" it will go or else it will not go, hence in this way it gives you to analyze the file sizes and all other factors before going to import a site.
Here is the script for this:

param([string]$Sourceurl,[string]$Destinationurl, [string]$FileName, [string]$Location, [switch]$help)

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

function GetHelp() {


$HelpText = @"

DESCRIPTION:
This script export a site from source site and then imports that at a destination url.

"@
$HelpText

}

function RahulExportImport-SPSite([string]$Sourceurl,[string]$Destinationurl, [string]$FileName, [string]$Location) {

if(Test-Path $Location) {

} else {
new-item -path $Location -type directory | Out-Null
}

$SPExport = New-Object Microsoft.SharePoint.Deployment.SPExport
        $SPImport = New-Object Microsoft.SharePoint.Deployment.SPImport
        $SPExportSettings = $SPExport.Settings
$SPExportSettings.SiteUrl= $Sourceurl
$SPExportSettings.BaseFilename = $FileName
$SPExportSettings.FileLocation = $Location
        $SPExportSettings.CommandLineVerbose = $true
        $SPExportSettings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]"ExportAll"
        $SPExportSettings.LogFilePath =$Location +"\logExport.log"

$SPExport.Run()
        write-host "The export is completed. Do you want to go for Import? If Yes then type y or else type anything else"
        $Decision = read-host
        if($Decision -eq "y")
        {
$SPImportSettings = $SPImport.Settings
        $SPImportSettings.SiteUrl= $Destinationurl
        $SPImportSettings.BaseFilename = $FileName
$SPImportSettings.FileLocation = $Location
        $SPImportSettings.CommandLineVerbose = $true
        $SPImportSettings.LogFilePath =$Location +"\logImport.log"
        $SPImport.Run()
         }

$SPExport.Dispose()
        $SPImport.Dispose()
       
}

if($help) { GetHelp; Continue }
if($Sourceurl -AND $Destinationurl -AND $FileName -AND $Location) { RahulExportImport-SPSite -Sourceurl $Sourceurl -Destinationurl $Destinationurl -FileName $FileName -Location $Location }

I hope this will help you out.

Thanks,
Rahul Rashu

No comments:

Post a Comment