The script synchronizes the list of allowed ActiveSync devices of a user from on-premises Active Directory to their Exchange Online mailbox. To run the script, you can create a custom command or scheduled task configured for the User object type.
PowerShell
# Get Microsoft 365 object ID
try
{
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
return # No Microsoft 365 account
}
# Get device IDs
try
{
$deviceIDs = $Context.TargetObject.GetEx("msExchMobileAllowedDeviceIDs")
}
catch
{
$deviceIDs = $NULL
}
# Connect to Exchange Online
$Context.CloudServices.ConnectExchangeOnline()
# Set devices
Set-CASMailbox -Identity $objectId.ToString() -ActiveSyncAllowedDeviceIDs $deviceIDs