Exchange PowerShell to mail-enable AD accounts

JRV 546 Reputation points
2021-12-15T23:21:50.277+00:00

Scenario: Adding Azure AD Connect to an existing AD domain with existing MS365 cloud-only mailboxes. AADC and EX2016 Management Server are set up with Minimal Hybrid Exchange, and everything is working with test accounts created in EAC as remote mailboxes.

However, once AAD syncs an existing AD account to AAD and soft-matches it, SSO will work, but the remote mailbox is not visible in on-prem EAC until the user account is AD-Enabled.

I had hoped to do something like this in Exchange Management Shell:

Get-User -OrganizationalUnit "OU=AAD.Sync,OU=Users,DC=[REDACTED],DC=Local" | Enable-RemoteMailbox -RemoteRoutingAddress $_.userPrincipalName

But I get this output:

The address '@redacted.mail.onmicrosoft.com' is invalid: "@redacted.mail.onmicrosoft.com" isn't a
valid SMTP address. The domain name can't contain spaces and it has to have a prefix and a suffix, such as example.com.
    + CategoryInfo          : NotSpecified: (:) [Enable-RemoteMailbox], DataValidationException
    + FullyQualifiedErrorId : [Server=REDACTED,RequestId=980e059c-1250-4de3-9b3d-27964d9cd9b1,TimeStamp=12/15/2021
   11:12:18 PM] [FailureCategory=Cmdlet-DataValidationException] EFF760F5,Microsoft.Exchange.Management.RecipientTask
  s.EnableRemoteMailbox
    + PSComputerName        : redacted.redacted.local

So I feel like I'm close...I'm just not getting the UPN; I'm getting only the tenancy name. I haven't figured out from the Get-User documentation how to find out how to get the userPrincipalName attribute.

I know I will also need to deal with the ExchangeGUID but haven't gotten that far yet! This will at least make the remote mailboxes manageable in EAC.

Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
1,896 questions
{count} votes

Accepted answer
  1. KyleXu-MSFT 26,211 Reputation points
    2021-12-16T06:48:47.443+00:00

    @JRV

    I don't quite understand what you are going to do. But, from the script that you provided, I guess you want to enable remote mailboxes for AD accounts which contained in a specific OU, if so, you could try with the script below:

    $users = Get-User -OrganizationalUnit "domain.com/ToOnline"  
    ForEach($user in $users){  
        $address = $user.Name+"@contoso.mail.onmicrosoft.com"  
        Enable-RemoteMailbox $user.Name -RemoteRoutingAddress $address   
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful