diff --git a/New-EOLMigration.ps1 b/New-EOLMigration.ps1 new file mode 100644 index 0000000..fbd742a --- /dev/null +++ b/New-EOLMigration.ps1 @@ -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:///powershell -Credential $login -Authentication Kerberos -AllowRedirection + Import-PSSession $exchSession -AllowClobber -CommandName Get-Mailbox,Enable-Mailbox + +} + +$email_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 '' ` + -TargetDeliveryDomain '' ` + -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 ' -To 'Test User ' ` + -Subject "$($batchName) To EOL Migration" -Body $($email_Body | Out-String) -BodyAsHtml ` + -DeliveryNotificationOption OnSuccess,OnFailure -SmtpServer 'Test User' + + $current_Progress | Format-Table + } + else { + $email_Body = $current_Progress | ConvertTo-Html -Head $email_Style + Send-MailMessage -From 'Test User ' -To 'Test User ' ` + -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 '' + + $current_Progress | Format-Table + break + } + #WAIT FOR 8 HOURS + Start-Sleep -Seconds 28800 + } +} \ No newline at end of file