Dynamic DL script

Hi All, its been a while :slight_smile:

i have knocked together a script to create dynamic DLs based on a CSV

CSV looks like below

CostCentre CostcentreName
99-001 Testing 1
99-002 Testing 2

Script here

$filepath = "c:\temp\QBDGCCs.csv" 
$CCs = Import-CSV $filepath 
#Collection of Cost Centre column

Foreach ($CC in $CCs)

$CCnumber = $cc.costcentre
$CCName = $cc.CostcentreName
$QBDGName = "$ccnumber $CCName"

#$Managedby = "Claire Colthirst"
#$Note = "New Future Org CC - created 21/02/22"

New-DynamicDistributionGroup -Name $QBDGName -OrganizationalUnit 'global.arup.com/Global/Dynamic Distribution Groups/Cost Centres' -Alias "$CCnumber-QBDG" -PrimarySmtpAddress "$CCnumber-QBDG@arup.com" -RecipientContainer 'global.arup.com’ -RecipientFilter { ((RecipientType -eq 'UserMailbox') -or (RecipientType -eq 'MailUser')) -and
(EmailAddresses -like '*@arup.mail.onmonmicrosoft.com') -and
(CustomAttribute15 -eq 'human:staff') -and
(Department -like "$CCnumber*") } }

$DG = Get-DynamicDistributionGroup "99-001 testing 1"
Get-Recipient -RecipientPreviewFilter $DG.RecipientFilter -OrganizationalUnit $DG.RecipientContainer -ResultSize unlimited

All runs fine however there is no members as i would expect, querying the filter i get

((((((((((RecipientType -eq 'UserMailbox') -or (RecipientType -eq 'MailUser'))) -and (EmailAddresses -like '*@xxxx.mail.onmonmicrosoft.com'))) -and (Custo
mAttribute15 -eq 'human:staff'))) -and (Department -like '$CCnumber*'))) -and (-not(Name -like 'SystemMailbox{*')) -and (-not(Name -like 'CAS_{*')) -and (
-not(RecipientTypeDetailsValue -eq 'MailboxPlan')) -and (-not(RecipientTypeDetailsValue -eq 'DiscoveryMailbox')) -and (-not(RecipientTypeDetailsValue -eq 
'PublicFolderMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'ArbitrationMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'AuditLogMailbox')) -and 
(-not(RecipientTypeDetailsValue -eq 'AuxAuditLogMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'SupervisoryReviewPolicyMailbox')))

Notice department (Department -like '$CCnumber*'))) why is this not populating the number, $ccnumber has the correct value as i use it for alias


Welcome to the forum. :wave:t4:

Just because I’m curious - why is just half of your code formatted as code? Could you please go back and correct that?

Thanks in advance.

Ahh as the second half was a result from the get-recipient query

In general: for very long command lines I’d urgently recommend using splatting as it makes your code way easier to write and to read. :point_up_2:t4:

Without access to an Exchange system only according to the documentation I’d say - since the parameter -RecipientFilter is of type string - not scriptblock I’d try it this way:

$CCs = Import-CSV -Path 'c:\temp\QBDGCCs.csv'

foreach ($CC in $CCs) {
    $CCnumber = $cc.costcentre
    $CCName = $cc.CostcentreName

    $NewDynamicDistributionGroupProps = @{
        Name               = "$ccnumber $CCName"
        OrganizationalUnit = 'global.arup.com/Global/Dynamic Distribution Groups/Cost Centres'
        Alias              = "$CCnumber-QBDG"
        PrimarySmtpAddress = "$CCnumber-QBDG@arup.com"
        RecipientContainer = 'global.arup.com'
        RecipientFilter    = "((RecipientType -eq 'UserMailbox') -or (RecipientType -eq 'MailUser')) -and (EmailAddresses -like '*@arup.mail.onmonmicrosoft.com') -and (CustomAttribute15 -eq 'human:staff') -and (Department -like '$CCnumber*')"
    New-DynamicDistributionGroup @NewDynamicDistributionGroupProps 

$DG = Get-DynamicDistributionGroup '99-001 testing 1'
Get-Recipient -RecipientPreviewFilter $DG.RecipientFilter -OrganizationalUnit $DG.RecipientContainer -ResultSize unlimited

Thanks, that sorted it. Its been a while since using PS let alone Exchange PS :smiley: