Add 'New-EOLMigration.ps1'

Signed-off-by: Tre Hayes <thayes@noreply.homeserver.local>
This commit is contained in:
Tre Hayes 2022-03-29 16:35:34 -05:00
parent 1381b555d1
commit 910ec1e441

97
New-EOLMigration.ps1 Normal file
View File

@ -0,0 +1,97 @@
param (
# Parameter help description
[Parameter(Mandatory=$true,ValueFromPipeline =$true)] [array] $Users,
# Parameter help description
[Parameter(Mandatory=$true)] [string] $batchName
)
function New-EOLSession {
param (
[ValidateNotNull()]
[System.Management.Automation.PSCredential] $login = [System.Management.Automation.PSCredential]::Empty
)
$eolSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $login -Authentication Basic -AllowRedirection
Import-PSSession $eolSession -AllowClobber -CommandName New-MoveRequest,Get-MoveRequest*,Remove-MoveRequest,New-MigrationBatch,`
Start-MigrationBatch,Get-MigrationBatch,Get-MigrationUserStatistics
}
function New-OnPremSession {
param (
[ValidateNotNull()]
[System.Management.Automation.PSCredential] $login = [System.Management.Automation.PSCredential]::Empty
)
$exchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<EXCHANGE_SERVER.local>/powershell -Credential $login -Authentication Kerberos -AllowRedirection
Import-PSSession $exchSession -AllowClobber -CommandName Get-Mailbox,Enable-Mailbox
}
$email_Style = "<style>BODY{font-family: Arial; font-size: 10pt;}TABLE{border: 1px solid black; border-collapse: collapse;}TH{border: 1px solid black; background: #dddddd; padding: 5px; }TD{border: 1px solid black; padding: 5px; }</style>"
$users_In_Batch = 0
$empEmails = foreach ($user in $Users) {
$users_In_Batch++
Get-ADUser -Identity $user.Identity -Properties EmailAddress,legacyExchangeDN,proxyAddresses | Sort-Object EmailAddress | Select-Object EmailAddress
}
$empEmails | Export-Csv -Path '.\OnBoarding_To_EOL_CSV.csv' -NoTypeInformation
#Session to EOL
$eolCred = Get-Credential -Message "EOL Credentials (ex. xxxxx@nkch.org)"
New-EOLSession -login $eolCred
if (Get-MigrationBatch -Identity $batchName) {
Write-Host -ForegroundColor Red "`nPlease Select a different batch name. These batch names have already been used:`n$(Get-MoveRequest | Select-Object -Unique BatchName | Out-String)"
break
}
else {
#KICK OFF MOVE REQUEST FOR EACH USER TO EOL
$OnboardingBatch = New-MigrationBatch -Name $batchName -SourceEndpoint '<LOCAL EXCHANGE ENDPOINT>' `
-TargetDeliveryDomain '<EOL TARGET DOMAIN>' `
-CSVData ([System.IO.File]::ReadAllBytes(".\OnBoarding_To_EOL_CSV.csv")) `
-AutoComplete
Start-MigrationBatch -Identity $OnboardingBatch.Identity.Id
#PRINT OUT ALL USERS' TRANSFER PROGRESS IF ANY USER IS NOT COMPLETED
while ($true) {
#CHECK IF STILL CONNECTED TO EXCHANGE. IF NOT, RECONNECT
try {
Get-MigrationBatch -Identity $batchName
}
catch {
New-EOLSession -login $eolCred
New-OnPremSession -login $onpremCred
}
#WAIT FOR 30 MINUTES
Start-Sleep -Seconds 1800
$progress_Count = 0
$current_Progress = foreach ($emp in $empEmails) {
Get-MigrationUserStatistics -Identity $emp.EmailAddress | Select-Object @{l='Mailbox';e={$_.Identity}},Status,EstimatedTotalTransferSize,BytesTransferred | Sort-Object Status -Descending
}
foreach ($progress in $current_Progress) {
if ('Completed' -eq $progress.Status.Value) { $progress_Count++ }
}
if ($progress_Count -lt $users_In_Batch) {
$email_Body = $current_Progress | ? {$_.Status.Value -ne 'Completed'} | ConvertTo-Html -Head $email_Style
Send-MailMessage -From 'Test User <Test.User@example.com>' -To 'Test User <Test.User@example.com>' `
-Subject "$($batchName) To EOL Migration" -Body $($email_Body | Out-String) -BodyAsHtml `
-DeliveryNotificationOption OnSuccess,OnFailure -SmtpServer 'Test User<SMTP_SERVER.local>'
$current_Progress | Format-Table
}
else {
$email_Body = $current_Progress | ConvertTo-Html -Head $email_Style
Send-MailMessage -From 'Test User <Test.User@example.com>' -To 'Test User <Test.User@example.com>' `
-Subject "$($batchName) To EOL Migration" `
-Body "All Users in this batch have completed the migration to EOL: `n`n$($email_Body | Out-String)" -BodyAsHtml `
-DeliveryNotificationOption OnSuccess,OnFailure -SmtpServer '<SMTP_SERVER.local>'
$current_Progress | Format-Table
break
}
#WAIT FOR 8 HOURS
Start-Sleep -Seconds 28800
}
}