Created
May 29, 2018 02:06
-
-
Save VibhuKuchhal/00a20ace419cba264e4effc81a7e9b8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Install-Module SharePointPnPPowerShellOnline -AllowClobber -Force | |
#Update-Module SharePointPnPPowerShell* | |
Import-Module Sharegate | |
$credentials = &'\\masterterminal\C$\AutomationScripts\GetCredentials.ps1' | |
$migrationDataFolder = '\\masterterminal\c$\AutomatedMigrationData' | |
$packagesFolder = 'packages' | |
$mailFolder = 'mails' | |
$reportsFolder = 'reports' | |
$inputFolder = 'input' | |
$processingFolder = 'processing' | |
$finalFolder = 'final' | |
$migrationConfigSiteUrl = 'https://yourtenant.sharepoint.com/sites/HSSPilot' | |
$migrationConfigList = 'MigrationList' | |
$migrationRoot = 'https://yourtenant.sharepoint.com/sites/' | |
function Send-MigrationCompletionMailTrigger{ | |
Param( | |
[string] $source, | |
[string] $destination, | |
[string] $LibraryName, | |
[System.DateTime] $startTime, | |
[System.DateTime] $endTime | |
) | |
$path = $migrationDataFolder + '\' + $mailFolder + '\' + $inputFolder + '\' + $endTime.ToString("yyyyMMdd-hhmmss") + "_" + $LibraryName + ".csv" | |
$subject = 'Migration Completed' | |
$body = '<h3>A Migration was completed</h3>' + | |
'<br/>Source : ' + $source + | |
'<br/>Destination : ' + $destination + | |
'<br/>Start Time: ' + $startTime.ToString("dd/MM/yyyy hh:mm:ss") + | |
'<br/>End Time: ' + $endTime.ToString("dd/MM/yyyy hh:mm:ss") + | |
'<br/>' | |
$props = @{ | |
Subject = $subject | |
Body = $body | |
} | |
$object = new-object psobject -Property $props | |
$object | Export-Csv -Path $path -NoTypeInformation | |
} | |
function Send-MigrationInitiatingMailTrigger{ | |
Param( | |
[string] $source, | |
[string] $destination, | |
[System.DateTime] $startTime, | |
[string] $LibraryName | |
) | |
$path = $migrationDataFolder + '\' + $mailFolder + '\' + $inputFolder + '\' + $startTime.ToString("yyyyMMdd-hhmmss") + "_" + $LibraryName + ".csv" | |
$subject = 'Migration Started' | |
$body = '<h3>A Migration was initiated</h3>' + | |
'<br/>Source : ' + $source + | |
'<br/>Destination : ' + $destination + | |
'<br/>Start Time: ' + $startTime.ToString("dd/MM/yyyy hh:mm:ss") + | |
'<br/>' | |
$props = @{ | |
Subject = $subject | |
Body = $body | |
} | |
$object = new-object psobject -Property $props | |
$object | Export-Csv -Path $path -NoTypeInformation | |
} | |
function Send-MigrationTerminalStartedMailTrigger{ | |
$path = $migrationDataFolder + '\' + $mailFolder + '\' + $inputFolder + '\' + (Get-Date).ToString("yyyyMMdd-hhmmss") + "_start_" + $env:COMPUTERNAME + ".csv" | |
$subject = 'Migration Terminal started' | |
$body = '<h3>A Migration terminal job was initiated</h3>' + | |
'<br/>Terminal Name: ' + $env:COMPUTERNAME + | |
'<br/>Time: ' + (Get-Date).ToString("dd/MM/yyyy hh:mm:ss") + | |
'<br/>' | |
$props = @{ | |
Subject = $subject | |
Body = $body | |
} | |
$object = new-object psobject -Property $props | |
$object | Export-Csv -Path $path -NoTypeInformation | |
} | |
function Send-MigrationTerminalEndMailTrigger{ | |
$path = $migrationDataFolder + '\' + $mailFolder + '\' + $inputFolder + '\' + (Get-Date).ToString("yyyyMMdd-hhmmss") + "_start_" + $env:COMPUTERNAME + ".csv" | |
$subject = 'Migration Terminal stopped' | |
$body = '<h3>A Migration terminal job was stopped</h3>' + | |
'<br/>Terminal Name: ' + $env:COMPUTERNAME + | |
'<br/>Time: ' + (Get-Date).ToString("dd/MM/yyyy hh:mm:ss") + | |
'<br/>' | |
$props = @{ | |
Subject = $subject | |
Body = $body | |
} | |
$object = new-object psobject -Property $props | |
$object | Export-Csv -Path $path -NoTypeInformation | |
} | |
function Send-MigrationFailureMailTrigger{ | |
Param( | |
[string] $source | |
) | |
$path = $migrationDataFolder + '\' + $mailFolder + '\' + $inputFolder + '\' + $startTime.ToString("yyyyMMdd-hhmmss") + "_" + $LibraryName + ".csv" | |
$subject = 'Migration Source not available' | |
$body = '<h3>An attempted Migration failed because the source was not available.</h3>' + | |
'<br/>Source : ' + $source + | |
'<br/>' | |
$props = @{ | |
Subject = $subject | |
Body = $body | |
} | |
$object = new-object psobject -Property $props | |
$object | Export-Csv -Path $path -NoTypeInformation | |
} | |
function Check-Site{ | |
Param( | |
[string] $SiteName, | |
[System.Management.Automation.CredentialAttribute()] $Credentials | |
) | |
Connect-SPOService -Url 'https://yourtenant-admin.sharepoint.com' -Credential $Credentials | |
$site = Get-SPOSite | Where-Object {$_.Url.ToLower() -eq ($migrationRoot + $siteName).ToLower()} | |
if(!$site) | |
{ | |
Connect-PnPOnline -Url $migrationConfigSiteUrl -Credentials $Credentials | |
New-PnPSite -Type TeamSite -Title $SiteName -Alias $SiteName -IsPublic | |
} | |
} | |
function Create-Library{ | |
Param( | |
[string] $SiteName, | |
[string] $LibName, | |
[System.Management.Automation.CredentialAttribute()] $Credentials | |
) | |
#Checking Site | |
Check-Site -SiteName $SiteName -Credentials $Credentials | |
#creating Site Url | |
$url = $migrationRoot + $siteName | |
Write-Host "Connecting... " $url " --- " $LibName | |
## Connecting to the site | |
$site = Connect-PnPOnline -Url $url -Credentials $Credentials | |
## Checking List | |
$list = Get-PnPList -Identity $LibName | |
if (!$list) | |
{ | |
## Creating list if not available | |
$list = New-PnPList -Title $LibName -EnableVersioning -Template DocumentLibrary -OnQuickLaunch | |
Write-Host "Created new library - " $LibName | |
} | |
} | |
function Extract-MigrationReport{ | |
Param ( | |
[string] $reportpath, | |
[string] $finalpath | |
) | |
$data = Import-Csv $reportpath | Where-Object {$_.Status -eq "Success" -and $_.Type -eq "File"} | Select-Object @{ expression={$_.'Source Path'}; label='SourceFilePath' } | Export-Csv $finalpath -NoTypeInformation | |
} | |
function Migrate-List{ | |
Param( | |
[System.Management.Automation.CredentialAttribute()] $Credentials, | |
[string] $SiteName, | |
[string] $LibName, | |
[string] $SourceFolder, | |
[bool] $migrationScope | |
) | |
Write-Host Initaiting Migration for Source: $SourceFolder for destination site: $SiteName and Library $LibName | |
$url = $migrationRoot + $siteName | |
$dstSite = Connect-Site -Url $url -Credential $Credentials | |
$dstList = Get-List -Site $dstSite -name $LibName | |
$startTime = Get-Date | |
Write-Host "Starting Upload Process - [$startTime]...." | |
#Sending migration initiation mail | |
Send-MigrationInitiatingMailTrigger -source $SourceFolder -destination ($url + '/'+ $LibName) -startTime $startTime -LibraryName $LibName | |
## Mapping Required properties and filters | |
$propTemplate = Import-PropertyTemplate -Path ($migrationDataFolder + '\templates\FilteredFilesTemplate.sgt') -List $dstList -Overwrite | |
$templateName = "FilteredFilesTemplate" | |
if($migrationScope) | |
{ | |
$propTemplate = Import-PropertyTemplate -Path ($migrationDataFolder + '\templates\AllFilesTemplate.sgt') -List $dstList -Overwrite | |
$templateName = "AllFilesTemplate" | |
} | |
$propMappings = Import-PropertyMapping -Path ($migrationDataFolder + '\templates\PropertyMappings.sgpm') | |
$propMappings = Import-UserAndGroupMapping -Path ($migrationDataFolder + '\templates\UserAndGroupMappings.sgum') -MappingSettings $propMappings | |
$result = Import-Document -DestinationList $dstList -SourceFolder $sourceFolder -TemplateName $templateName -MappingSettings $propMappings -WaitForImportCompletion | |
$endTime = Get-Date | |
Write-Host "Finished Upload Process - [$endTime]." | |
$reportPath = $migrationDataFolder + '\' + $reportsFolder + '\Consolidated\' + $endTime.ToString("yyyyMMdd-HHmmss") + "_" + $env:computername + "_" + $result.SessionId + "_" + $LibName + ".csv" | |
Export-Report $result -Path $reportPath | |
$extractPath = $migrationDataFolder + '\' + $reportsFolder + '\Extracted\' + $endTime.ToString("yyyyMMdd-HHmmss") + "_Cleanup_" + $LibName + ".csv" | |
Extract-MigrationReport -reportpath $reportPath -finalpath $extractPath | |
#Sending migration initiation mail | |
Send-MigrationCompletionMailTrigger -source $SourceFolder -destination ($url + '/'+ $LibName) -LibraryName $LibName -startTime $startTime -endTime $endTime | |
} | |
function Execute-Migration{ | |
Param( | |
[string] $sourcePath, | |
[String] $destinationSite, | |
[String] $destinationlibrary, | |
[bool] $migrationScope, | |
[System.Management.Automation.CredentialAttribute()] $Credentials | |
) | |
Create-Library -siteName $destinationSite -libName $destinationlibrary -Credentials $Credentials | |
Write-Host $sourcePath | |
Migrate-List -Credentials $Credentials -SiteName $destinationSite -LibName $destinationlibrary -SourceFolder $sourcePath -migrationScope $migrationScope | |
} | |
function Execute-CheckandMigrate{ | |
Param( | |
[System.Management.Automation.CredentialAttribute()] $Credentials | |
) | |
$inputpath = $migrationDataFolder + '\' + $packagesFolder + '\' + $inputFolder | |
$processingPath = $migrationDataFolder + '\' + $packagesFolder + '\' + $processingFolder | |
$unprocessedRecords = Get-ChildItem -Path $inputpath | |
if($unprocessedRecords) | |
{ | |
$fileItem = $unprocessedRecords[0] | |
Move-Item -Path $fileItem.FullName -Destination $processingPath | |
$effectivepath = $migrationDataFolder + '\' + $packagesFolder + '\' + $processingFolder + '\' + $fileItem.Name | |
$effectivepath | |
$processingitem = Get-Item -Path $effectivepath | |
$data = Import-Csv -Path $effectivepath | |
$data | |
Connect-PnPOnline -Url $migrationConfigSiteUrl -Credentials $Credentials | |
Write-Host "Trying to find item with ID: " $data.Id | |
$listItem = (Get-PnPListItem -List $migrationConfigList -Id $data.Id -Fields "Id","Source","DestinationSite","DestinationLibrary","TerminalName","MigrateAllData").fieldvalues | |
$listItem | |
$Source = $listItem.Source | |
$destination = $listItem.DestinationSite | |
$destinationLibrary = $listItem.DestinationLibrary | |
$alldata = $false | |
$terminal = $listItem.TerminalName | |
#Check Source | |
$content = Get-ChildItem -Path $source -ErrorAction Ignore | |
if(!$content) | |
{ | |
Send-MigrationFailureMailTrigger -source $source | |
} | |
else | |
#Proceed to migration if source is available | |
{ | |
if($listItem.MigrateAllData -eq "True") | |
{ | |
$alldata = $true | |
} | |
if(!$terminal) | |
{ | |
Write-Host "Initiating Migration Procedure" | |
#Locking out the item / stamping migration item with Local Computer name | |
Set-PnPListItem -List $migrationConfigList -Identity $data.Id -Values @{"Started" = $true; "TerminalName"=$env:COMPUTERNAME} | |
## Executing Migration | |
Execute-Migration -sourcePath $Source -destinationSite $destination -destinationlibrary $destinationLibrary -migrationScope $alldata -Credentials $Credentials | |
## Logging finalization Status | |
Connect-PnPOnline -Url $migrationConfigSiteUrl -Credentials $Credentials | |
Set-PnPListItem -List $migrationConfigList -Identity $data.Id -Values @{"Migrated" = $true} | |
## Finalizing the migration package | |
$finalPath = $migrationDataFolder + '\' + $packagesFolder + '\' + $finalFolder | |
Move-Item -Path $processingitem -Destination $finalPath | |
} | |
} | |
} | |
} | |
Send-MigrationTerminalStartedMailTrigger | |
while($true){ | |
if((Get-Date).Hour -lt 22) | |
{ | |
Write-Host "Checking..." | |
Execute-CheckandMigrate -Credentials $credentials | |
Write-Host "Sleeping for 1 Minute" | |
Start-Sleep -Seconds 60 | |
} | |
else | |
{ | |
Write-Host 'Stopping Migration Terminal task.' | |
Start-Sleep 10 | |
Send-MigrationTerminalEndMailTrigger | |
Exit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment