forked from mardahl/PSBucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-20HybridExchangeMigrationBatches.ps1
More file actions
115 lines (90 loc) · 4.39 KB
/
Copy pathCreate-20HybridExchangeMigrationBatches.ps1
File metadata and controls
115 lines (90 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<#
.SYNOPSIS
Creates about 20 Exchange Online Migration batches containing all the eligeble users in the On-prem Exchange Organization
.DESCRIPTION
Can be used by lazy admins to simply create and start about 20 migration batches containing all the user synced
to Office 365 with the "MailUser" recipient type set.
UPN's containing onmicrosoft.com will be skipped!
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
Run the script without any parameters
.\Create-20HybridExchangeMigrationBatches.ps1
.LINK
Links to further documentation.
.NOTES
The script must be run interactively.
This script requires the Exchange Online V2 Powershell module, which it will try to install.
Modify the declarations region of the script in order to succeed with the execution.
Licensed under MIT, feel free to modify and distribute freely, but leave author credits.
Created by Michael Mardahl
Twitter: @michael_mardahl
Github : github.com/mardahl
#>
#region Declarations
$TargetDeliveryDomain = "TENANTNAME.mail.onmicrosoft.com"
$notificationEmail = "myUser@domain.com"
$badItemLimit = "1000"
$csvTempPath = ".\CSVfiles"
#endregion Declarations
#region Execute
Write-Verbose "Connecting to Exchange Online" -Verbose
try {
#Verify Exchange Online V2 modules are installed
if(!(Get-InstalledModule ExchangeOnlineManagement)){
Install-Module ExchangeOnlineManagement -Force
}
#Connect to exchange online using modern authentication
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop
} catch {
Throw "Could not connect to exchange online, make sure to get the exchange online v2 module installed"
}
Write-Verbose "Connecting to Exchange Online completed." -Verbose
#Build sorted list of users
Write-Verbose "Building list of users that can be migrated." -Verbose
$batchUsers = Get-User | Where-Object { $_.RecipientType -eq "MailUser" -and $_.Name -notlike "Health*" -and $_.IsDirSynced -eq $true -and $_.UserPrincipalName -notlike "*onmicrosoft.com" } | select UserPrincipalName -ExpandProperty UserPrincipalName | sort UserPrincipalName
Write-Verbose "List creation completed." -Verbose
#Calculate how many users need to be in each batch to fit them all in 20 batches
$batchMembersCount = [math]::floor($batchUsers.Count / 20)
Write-Verbose "Creating temporary CSV files for batch import." -Verbose
#Clear out old CSV files and create temp directory
if (Test-Path $csvTempPath) { Remove-Item $csvTempPath -Recurse -Force}
$createDir = New-Item -ItemType Directory -Path $csvTempPath
#Create the batch csv files
$upnCount = 0
$fileCount = 1
foreach($upn in $batchUsers){
if($upnCount -eq 0) {
#Create temporary CSV file on first iteration
$csvTempFile = Join-Path $csvTempPath "\batch$($fileCount).csv"
Add-Content -Path $csvTempFile -Value 'EmailAddress'
}
#add users UPN to the initial and subsequent iterations
Add-Content -Path $csvTempFile -Value "$upn"
$upnCount++
if($batchMembersCount -eq $upnCount){
#reset iteration as we have reached the calculated maximum users of this batch.
$upnCount = 0
#Increase filename for next batch iteration.
$fileCount++
}
}
Write-Verbose "CSV file creation completed." -Verbose
#Create migration batches in Exchange Online
Write-Verbose "Opening migration endpoint select window..." -Verbose
$SourceEndpoint = Get-MigrationEndpoint | Out-GridView -Title "Select 1 migration endpoint for batches..." -PassThru | select Identity -ExpandProperty Identity
Write-Verbose "Adding $fileCount batches to Exchange Online and starting initial sync." -Verbose
$batchCount = 1
Do {
#Create migration batch for each CSV
$csvTempFile = Join-Path $csvTempPath "\batch$($batchCount).csv"
$niceNumber = ([string]$batchCount).PadLeft(2,'0')
New-MigrationBatch -Name "AutoBatch$($niceNumber)" -SourceEndpoint "$SourceEndpoint" -CSVData ([System.IO.File]::ReadAllBytes("$csvTempFile")) -AutoStart -BadItemLimit $badItemLimit -TargetDeliveryDomain $TargetDeliveryDomain -NotificationEmails $notificationEmail -WhatIf
$batchCount++
} While ($batchCount -ne $fileCount)
Write-Verbose "Completed batch creation in Exchange Online." -Verbose
Disconnect-ExchangeOnline -Confirm:$false
Write-Verbose "End script." -Verbose
#endregion Execute