Tuesday, January 5, 2016

Office365 Dynamic Distribution Group creation with Multiple Custom Attributes

Dynamic distribution groups are mail-enabled Active Directory group objects that are created to expedite the mass sending of email messages and other information within a Microsoft Exchange organization.
Unlike regular distribution groups that contain a defined set of members, the membership list for dynamic distribution groups is calculated each time a message is sent to the group, based on the filters and conditions that you define. When an email message is sent to a dynamic distribution group, it’s delivered to all recipients in the organization that match the criteria defined for that group.

Use Case

Using Office356 Admin GUI you can create a multiple conditions in custom attribute to filter the AD group objects









Solution
Here you will find a PowerShell script functions code snippet example how to achieve this


 $name - string object your group name

$custom - string object with values separated by semicolon ";" "Test1; Test2; Test2" 

log - is a function for logging messages, you will need to implement it by yourself

function CreateDynamicDistributionGroup($name, $custom)
{
      log("Creating Dynamic distributiongroup " + $name)
      $result = $false;
      $custom = $null
     
    try{
        $results = Get-DynamicDistributionGroup -Identity $name -ErrorAction SilentlyContinue

        if($results -eq $null)
        {
            # Create new DynamicDistributionGroup
            $displayName = $name
            $alias = CreateFriendlyAlias($name)

                  $custom= FormatDynamicPropertyArray($custom)
                  $result = New-DynamicDistributionGroup -Name $name -Alias $alias -IncludedRecipients "MailboxUsers"

            $result = Set-DynamicDistributionGroup -Identity $alias -ManagedBy $appSettings["DefaultDistributionGroupOwner"] -Notes $notes
                  $result = Set-DynamicDistributionGroup -Identity $alias -IncludedRecipients "MailboxUsers" -ConditionalCustomAttribute1 $custom

            $result = $true
        }
        else
        {
            log("DistributionGroup " + $name + " already exist")
        }
    }
    catch [Exception]{
        log("Error creating distributiongroup " + $name + " Technical error: " + $_.Exception.Message)
    }

    return $result
}

function FormatDynamicPropertyArray($value)
{
      $newValueArray = @();

    if($value)
    {
          $values = $value -split ";"

              $values | ForEach-Object{
                        $newValueArray += $_.ToString()
            }
    }

      return $newValueArray

}

Have a good day! :)