The script updates ExchangeGuid of a user remote mailbox with the GUID of the mailbox in Exchange Online. To run the script, use a custom command, business rule or scheduled task configured for the User object type.
PowerShell
try
{
# Get the object ID in Microsoft 365
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
return # The user doesn't have a Microsoft 365 account
}
try
{
# Connect to Exchange Online
$session = $Context.CloudServices.CreateExchangeOnlinePSSession()
Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Get-Mailbox"
# Get mailbox
$mailbox = Get-Mailbox $objectId.ToString()
# Update ExchangeGuid
$Context.TargetObject.Put("msExchMailboxGuid", $mailbox.ExchangeGuid.ToByteArray())
$Context.TargetObject.SetInfo()
}
finally
{
# Close the remote session and release resources
if ($session) { Remove-PSSession $session }
}