TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Powershell Script to Automate Export and Import for a SharePoint 2010 Site
Karthik Muthu Karuppan
Feb 04
2015
Code
3.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
ExportImport.zip
The script requires input CSV file which has the details of source and destination URL's. Download the attached file to get the script and the CSV file format.
The script triggers you an email once the export and import completes. You need not wait monitoring the powershell window. Provide SMTP, From, Reply to and To addresses in the script.
Script
$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
$LogFile =
".\ExportImportPatch-$LogTime.rtf"
# Add SharePoint PowerShell Snapin
if
( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $
null
) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
#Deleting any .rtf files in the scriptbase location
$FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
if
($FindRTFFile)
{
foreach($file
in
$FindRTFFile)
{
remove-item $file
}
}
start-transcript $logfile
Function ExportImport([String]$SourceURL, [String]$DestinationURL)
{
$Web = get-spweb $SourceURL
$WebName = $Web.ID.GUID
write-host $webname
Write-host
"Creating folder for the export"
-fore Magenta
New-Item $scriptbase\$WebName -type directory
Write-host
"folder created for the export"
-fore Magenta
$ExportPath = $scriptbase +
"\" + $WebName + "
\
" + $webname + "
.cmp"
write-host
"Export path is "
$ExportPath -fore cyan
Write-host
"Performing export for the site "
$SourceURL -fore yellow
Export-spweb -identity $SourceURL -path $ExportPath -IncludeVersions all -IncludeUserSecurity
write-host
"Export completed"
-fore green
Write-host
"Performing import on site "
$DestinationURL -fore yellow
Import-SpWeb -identity $DestinationURL -path $ExportPath -IncludeUserSecurity
Write-host
"Import completed for the site "
$DestinationURL -fore green
Write-host
"Deleting folder"
$scriptbase\$WebName
remove-item $scriptbase\$WebName\* -exclude *.log -recurse -confirm:$
false
write-host
"Folder deleted successfully"
}
function
sendMail{
Write-Host
"Sending Email"
#SMTP server name
$smtpServer =
"SMTP address"
#Creating a Mail object
$msg =
new
-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp =
new
-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From =
"
[email protected]
"
$msg.ReplyTo =
"
[email protected]
"
#For multiple to address, seperate the address with comma
$msg.To.Add(
"
[email protected]
,
[email protected]
"
)
$msg.subject =
"Export/Import Completed"
$msg.body =
"Export/Import Completed."
#Sending email
$smtp.Send($msg)
}
write-host
"Please place the ExportImportDetails.csv under the folder where the powershell script is placed"
-fore yellow
sleep(3)
$ans = read-host
"Did you place the file? (y/n) "
if
($ans -eq
'y'
)
{
$csvfile = $scriptbase +
"\" + "
ExportImportDetails.csv"
import
-csv $csvfile | where {
ExportImport $_.SourceURL $_.DestinationURL
}
sendMail
}
else
{
write-host
"The user hasn't placed the file and choosen to exit"
-fore cyan
}
stop-transcript
SharePoint
PowerShell
Script