01 December 2021

Move new mailbox from on-premises to Exchange Online with PowerShell - Exchange Hybrid new mailbox moves

If your running Exchange Hybrid then you need to create your mailboxes on premises first and then sync them to AzureAD/Office365/EXO. 

Now in my case this was a manual task, and ofcourse this was forgotten too often.
So I created this script, to automate the move.
I'm still looking for the way to let the moves be visible in the EAC, it's something with New-MigrationBatch and Start-MigrationBatch.
I just didn't find the time yet to mess with it.

If you have the right method to make this possible, drop a line below.

For now this is how I do it (this needs to run from an Exchange server on-premises or a management server with the Exchange Management Tools installed):

 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
# Load Exchange modules and snapin
add-pssnapin microsoft.exchange.management.powershell.snapin
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1

# Connect to local Exchange server
Connect-ExchangeServer -auto -AllowClobber

# Import EXO module
Import-Module -Name ExchangeOnlineManagement

# Get the current date
$Date = Get-Date -format dd-MM-yyyy

# Get mailboxes created in the last 7 days
$mailboxes = Get-Mailbox | Where-Object {$_.WhenCreated ge ((Get-Date).Adddays(-7))} `
| Select UserPrincipalName,identity,samaccountname

# Get on-prem credentials
$onpremcred = Get-Credential domain\username

# Set the proxy for the connection to EXO
netsh winhttp set proxy proxy-server="http=proxy.domain.lan;https=proxy.domain.lan:8080" `
bypass-list="*.domain.lan;10.*"

# Connect to EXO
Connect-ExchangeOnline -UserPrincipalName username@domain.nl

# Show the mailboxes to move
$mailboxes

# Create a move request per found mailboxes
foreach ($mailbox in $mailboxes)
{
$username = $mailbox.Samaccountname
$moverequestbatch = New-MoveRequest -Identity $mailbox.SamAccountName -remote `
-RemoteHostName hybridserver.domain.nl -TargetDeliveryDomain tenantname.mail.onmicrosoft.com `
-RemoteCredential $OnPremCred -BatchName "MigrationService:$username"

<# === This piece is experimental ===
#$migrationEndpointOnPrem = tenantname.domain.nl-endpoint
#$OnboardingBatch = New-MigrationBatch -Name $username -SourceEndpoint `
$MigrationEndpointOnprem.Identity -TargetDeliveryDomain tenantname.mail.onmicrosoft.com `
-CSVData ([System.IO.File]::ReadAllBytes("C:\Users\Administrator\Desktop\RemoteOnBoarding1.csv"))

#Start-MigrationBatch -AutoStart -Name -AutoComplete -Identity $OnboardingBatch.Identity
#>
}

# Reset the proxy
netsh winhttp reset proxy

<#
# Some command to check your moves
Get-MoveRequest
Get-moverequest | Get-moverequeststatistics
Get-MoveRequest | Select batchname
Get-MoveRequest -MoveStatus Completed | Remove-MoveRequest -confirm:$false
Get-MoveRequest -MoveStatus CompletedWithWarning | Remove-MoveRequest -confirm:$false
#>

No comments:

Post a Comment