Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Powershell Script to Automate Export and Import for a SharePoint 2010 Site
WhatsApp
Karthik Muthu Karuppan
Feb 04
2015
3.7
k
0
0
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
Up Next
Powershell Script to Automate Export and Import for a SharePoint 2010 Site