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