Exchange PowerShell script to convert DL to contact

Recently I have came across a requirement where customer want to remove few of the DL but they want to keep the email-id mapped to a new contact. All the existing attribute should be stamped to the new contact. LegacyDN from DL should be stamped as X500 address onto the newly created contact. So that if someone send email to removed DL it should get forwarded to an external contact. Below is the script I wrote to convert the DL to a mail contact.

# Save the script as distributiongroupToContact.ps1 and run below cmdlt. Please do specify 
# the ExternalEmailAddress in the cmdlt as shown below.

# Usage: .\distributiongroupToContact.ps1 -Identity testdl -ExternalEmailAddress   
testdl@cloudmechanics.onmicrosoft.com -Confirm:$false



[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High"
)]
Param
(
[Parameter(Mandatory=$true,Position=0)]
[String]$Identity,
[Parameter(Mandatory=$true,Position=1)]
[String]$ExternalEmailAddress,
[Parameter(Mandatory=$false,Position=2)]
[String]$DomainController
)
PROCESS
{$parameters = @{}
if( $DomainController ) {
    $parameters.Add( 'DomainController', $DomainController )
    Write-Verbose ( 'Using Domain Controller "{0}"' -f $DomainController )
}

Write-Verbose ( 'Trying to find Distribution group for "{0}"' -f $Identity )
$Distributiongroup = get-distributiongroup $Identity @parameters -ErrorAction SilentlyContinue

if( ($Distributiongroup) -and -not ($Distributiongroup.count -gt 1) ) {
    if( $pscmdlet.ShouldProcess($Distributiongroup.DistinguishedName) ) {
        $parameters.add( 'Identity', $Distributiongroup.DistinguishedName )

        Write-Verbose ( 'Removing DL"{0}"' -f $Distributiongroup.DistinguishedName )
        remove-distributiongroup @parameters -confirm:$false -BypassSecurityGroupManagerCheck

        $parameters.add( 'ExternalEmailAddress', $ExternalEmailAddress )

        Write-Verbose ( 'Enabling mailuser "{0}"' -f $Distributiongroup.DistinguishedName )
        new-mailcontact $Distributiongroup.name -OrganizationalUnit $distributiongroup.OrganizationalUnit -ExternalEmailAddress $ExternalEmailAddress

        $parameters.add( 'EmailAddressPolicyEnabled', $Distributiongroup.EmailAddressPolicyEnabled )

        $EmailAddresses= @( 'x500:{0}' -f $Distributiongroup.legacyExchangeDN )

        if( $Distributiongroup.EmailAddressPolicyEnabled ) {
            foreach( $EmailAddress in $Distributiongroup.EmailAddresses ) {
                if( $EmailAddress -like 'smtp:*' ) {
                    $EmailAddresses += $EmailAddress.ToString().ToLower()
                } else {
                    $EmailAddresses += $EmailAddress.ToString()
                }
            }

            $parameters.add( 'EmailAddresses', @{Add=$EmailAddresses} )
        } else {
            foreach( $EmailAddress in $Distributiongroup.EmailAddresses ) {
                $EmailAddresses += $EmailAddress.ToString()
            }



            if( $Distributiongroup.EmailAddressPolicyEnabled ) {
            foreach( $EmailAddress in $Distributiongroup.EmailAddresses ) {
                if( $EmailAddress -like 'X500:*' ) {
                    $EmailAddresses += $EmailAddress.ToString().ToLower()
                } else {
                    $EmailAddresses += $EmailAddress.ToString()
                }
            }

            $parameters.add( 'EmailAddresses', @{Add=$EmailAddresses} )
        }



            $parameters.add( 'EmailAddresses', $EmailAddresses )
        }

        $parameters.add( 'DisplayName', $Distributiongroup.DisplayName )
        $parameters.add( 'Alias', $Distributiongroup.Alias )
        $parameters.add( 'CustomAttribute1', $Distributiongroup.CustomAttribute1 )
        $parameters.add( 'CustomAttribute2', $Distributiongroup.CustomAttribute2 )
        $parameters.add( 'CustomAttribute3', $Distributiongroup.CustomAttribute3 )
        $parameters.add( 'CustomAttribute4', $Distributiongroup.CustomAttribute4 )
        $parameters.add( 'CustomAttribute5', $Distributiongroup.CustomAttribute5 )
        $parameters.add( 'CustomAttribute6', $Distributiongroup.CustomAttribute6 )
        $parameters.add( 'CustomAttribute7', $Distributiongroup.CustomAttribute7 )
        $parameters.add( 'CustomAttribute8', $Distributiongroup.CustomAttribute8 )
        $parameters.add( 'CustomAttribute9', $Distributiongroup.CustomAttribute9 )
        $parameters.add( 'CustomAttribute10', $Distributiongroup.CustomAttribute10 )
        $parameters.add( 'CustomAttribute11', $Distributiongroup.CustomAttribute11 )
        $parameters.add( 'CustomAttribute12', $Distributiongroup.CustomAttribute12 )
        $parameters.add( 'CustomAttribute13', $Distributiongroup.CustomAttribute13 )
        $parameters.add( 'CustomAttribute14', $Distributiongroup.CustomAttribute14 )
        $parameters.add( 'CustomAttribute15', $Distributiongroup.CustomAttribute15 )
        $parameters.add( 'ExtensionCustomAttribute1', $Distributiongroup.ExtensionCustomAttribute1 )
        $parameters.add( 'ExtensionCustomAttribute2', $Distributiongroup.ExtensionCustomAttribute2 )
        $parameters.add( 'ExtensionCustomAttribute3', $Distributiongroup.ExtensionCustomAttribute3 )
        $parameters.add( 'ExtensionCustomAttribute4', $Distributiongroup.ExtensionCustomAttribute4 )
        $parameters.add( 'ExtensionCustomAttribute5', $Distributiongroup.ExtensionCustomAttribute5 )

        Write-Verbose ( 'Updating Mailcontact "{0}"' -f $Distributiongroup.DistinguishedName )
        set-mailcontact @parameters
    }
} elseif( $Distributiongroup.Count -gt 1 ) {
    Write-Host ( 'Multiple DL found for "{0}"' -f $Identity ) -ForegroundColor Red
} else {
    Write-Host ( 'Unable to find DL for "{0}"' -f $Identity ) -ForegroundColor Red
}
}

This entry was posted in Exchange Online, Powershell. Bookmark the permalink.

Leave a Reply

Your email address will not be published.