Search code examples
powershelloffice365exchange-server-2010

Create new Shared Mailbox in Office 365


I'm trying to write a script to created a remote shared mailbox but its not working.

following error show when try to convert it. Cannot process argument transformation on parameter 'Type'. Cannot convert value "Shared" to type "Microsoft.Exchange.Management.RecipientTasks.ConvertibleRemoteMailboxSubType" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Regular, Room, Equipment".

We are running Exchange 2010 hybird.

Connect-Hybrid

$SharedMailboxOU= "Shared Mailbox OU"
$SharedMailboxAccountOU = "Shared Mailbox Account OU"

$DisplayName=$mailboxName = Read-Host "Enter Mailbox Name"

$Alias = $mailboxName.replace(" ","_")
$GroupName = ("SM_" + $Alias)

$has_mailbox_found = Get-User $mailboxName -ErrorAction SilentlyContinue

if($has_mailbox_found){
    Write-Host "Account already existed"
    exit;
}else{

    $group = New-DistributionGroup -Type Security -OrganizationalUnit $SharedMailboxOU -Name $GroupName
    if($group) { Set-DistributionGroup $GroupName -HiddenFromAddressListsEnabled $true }

    #$mailbox = New-RemoteMailbox -Name $DisplayName -Alias $Alias -OnPremisesOrganizationalUnit $SharedMailboxAccountOU

    $password = ConvertTo-SecureString -String ("Password@@HHHSHSHSS") -AsPlainText -Force  

    $mailbox =New-RemoteMailbox -UserPrincipalName "[email protected]" `
        -DisplayName $Alias `
        -Name $Alias 
        -OnPremisesOrganizationalUnit $SharedMailboxAccountOU `
        -Alias $Alias `
        -Password $password

    if($group -and $mailbox) {
        Add-MailboxPermission -Identity $Alias `
                    -User $GroupName -AccessRights FullAccess -InheritanceType All

        Set-RemoteMailbox -Identity $Alias -Type Shared

        $User_Identity = $mailbox.DistinguishedName
        $User_Identity
        #Move-ADObject -Identity $User_Identity -TargetPath $SharedMailboxAccountOU
    }

}

Solution

  • Note the comments made by Microsoft about the Shared Type:

    Notes on the value Shared:

    Shared is available only in Exchange 2013 CU21 or later and Exchange 2016 CU10 or later. To use this value, you also need to run setup.exe /PrepareAD. For more information, see KB4133605.

    You can only use Shared on a mailbox that was originally created in Exchange Online. If the mailbox was originally created in on-premises Exchange and then migrated to Exchange Online, you need to migrate the mailbox back to on-premises Exchange, convert the mailbox to a shared mailbox in on-premises Exchange, and then migrate the shared mailbox to Exchange Online. Or, you can change the RemoteRecipientType property value of the on-premises mailbox to "ProvisionMailbox, Migrated, DeprovisionArchive" by running the following command on an Exchange server: Set-ADUser -Identity "" -Replace @{msExchRemoteRecipientTYpe="21"}.

    Source: https://learn.microsoft.com/en-us/powershell/module/exchange/federation-and-hybrid/set-remotemailbox?view=exchange-ps