Reputation: 1690
I am wanting to create some Dynamic Distribution Lists via a csv import. I want the -RecipientFilter to use values from the csv data. So, I am trying to work out the syntax and having no luck. My actual filter will have a few parts but I cannot even get this simple one to work.
Setting the -Name via a variable works fine.
$dgName = "AAC"
$dgCode1 = "QQ"
New-DynamicDistributionGroup -Name $dgName -RecipientFilter { ExtensionCustomAttribute1 -eq 'QQ' }
Setting the -RecipientFilter expression does not.
New-DynamicDistributionGroup -Name $dgName -RecipientFilter { ExtensionCustomAttribute1 -eq $dgCode1 }
as I end up with '$dgCode1'
in the filter.
I tried Invoke-Expression:
$myCommand = "New-DynamicDistributionGroup -Name $dgName -RecipientFilter { ExtensionCustomAttribute1 -eq $dgCode1 }"
Invoke-Expression $myCommand
but that throws "Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "Invalid filter syntax. For a description of the filter parameter syntax see the command help."
I have read everything I can find but cannot find a way to do this. I even tried string concatenation:
$myCommand = "New-DynamicDistributionGroup -Name $dgName -RecipientFilter { ExtensionCustomAttribute1 -eq " + $dgCode1 + " }"
but that still threw the "Invalid Filter Syntax" error.
How can this be done please? I am very new to PowerShell.
Thanks, Murray
EDIT: Thanks and Kudos to @AdminOfThings
A more complete example that works to replicate what I was trying to achieve (line beak for legibility):
$dgName = "Class2-3Parents"
$dgCode1 = "P"
$dgSubCode1 = "Class2"
$dgSubCode2 = "Class3"
New-DynamicDistributionGroup -Name $dgName -RecipientFilter
"ExtensionCustomAttribute1 -eq '$dgCode1' -and ( ExtensionCustomAttribute2 -eq '$dgSubCode1' -or ExtensionCustomAttribute2 -eq '$dgSubCode2' ) "
results in a filter of:
((ExtensionCustomAttribute1 -eq 'P') -and (((ExtensionCustomAttribute2 -eq 'Class2') -or (ExtensionCustomAttribute2 -eq 'Class3')))) ...
So grateful. Murray
Upvotes: 0
Views: 3073
Reputation: 25001
This appears to be an issue with using script block notation for a filter. All of the Microsoft help pages show using script blocks ({}
) for OPATH filters (used by Exchange commands) and Active Directory filters (used by ActiveDirectory module), and it is an incorrect practice because they are not script blocks. The following will have better results.
New-DynamicDistributionGroup -Name $dgName -RecipientFilter "ExtensionCustomAttribute1 -eq '$dgCode1'"
Certain script blocks will have their own scope. That scope won't know anything about variables created outside of the scope.
Upvotes: 1