I can replicate this issue. If I use the DN in a variable it does not work but if I copy the variable value and manually copy it into the new-DynamicDistributionGroup command it works correctly. Has anyone discovered why this is the case? it seams to be a bug in the New-DynamicDistributionGroup command. For example if you include the DN as a variable like this:
$DB = (get-mailboxdatabase $DBStr).distinguishedName New-DynamicDistributionGroup "EXDB $DBStr" -RecipientFilter {((Database -eq $DB) -and (RecipientType -eq 'UserMailbox'))} -RecipientContainer "DC=<domain>,DC=com" -OrganizationalUnit "OU=DL,OU=<OU>,DC=<domain>,DC=<domain>,DC=com"
it is not translated correctly during creation of the group. If you look at the recipients filter value after creating with a variable you will get something similar to this:
((((-not(Database -ne $null)) -and (RecipientType -eq 'UserMailbox'))) -and (-not(Name -like 'SystemMailbox{*')) -and (-not(Name -like 'CAS_{*')) -and (-not(RecipientTypeDetailsValue -eq 'MailboxPlan')) -and (-not(RecipientTypeDetailsValue -eq 'DiscoveryMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'ArbitrationMailbox')))
Note the ((-not(Database -ne $null)) part of the filter. I also tried to enclose the variable in quotes and various combinations and when I did I would get results like this:
$DB = (get-mailboxdatabase $DBStr).distinguishedName New-DynamicDistributionGroup "EXDB $DBStr" -RecipientFilter {((Database -eq "$DB") -and (RecipientType -eq 'UserMailbox'))} -RecipientContainer "DC=<domain>,DC=com" -OrganizationalUnit "OU=DL,OU=<OU>,DC=<domain>,DC=<domain>,DC=com"
Note the only change is quotes around the variable $DB. When you look at the recipients filter again you will see the following results:
((((Database -eq 'DC=$DB') -and (RecipientType -eq 'UserMailbox'))) -and (-not(Name -like 'SystemMailbox{*')) -and (-not(Name -like 'CAS_{*')) -and (-not(RecipientTypeDetailsValue -eq 'MailboxPlan')) -and (-not(RecipientTypeDetailsValue -eq 'DiscoveryMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'ArbitrationMailbox')))
You can see now the (Database -eq 'DC=$DB') doesn't have the -not or $null but instead has DC= in front of the variable name itself. This is contrary to normal Powershell commands in more ways than one. Any other command where
I include a variable in double quotes the variable is replaced with the value but not here. I've tried various methods to explicitly define the variable as a string with no better results or it generates an error. The only solution at this point
is to manually create the New-DynamicDistributionGroup command and paste the DN value into the command. This has been tested and reproduced with Powershell 2.0 and 3.0 running on Win7 x64 with Exchange Management tools installed.
All of my exchange 2010 servers are patched up to the current version. We do still have Exchange 2003 servers in our enviroment but no 2007 servers. For reference Exchange 2010 version info below:
ServerRole : Mailbox, ClientAccess, HubTransport
AdminDisplayVersion : Version 14.2 (Build 247.5)
ExchangeVersion : 0.1 (8.0.535.0)
If you would like any additional information to help troubleshoot this issue please let me know. If I find another solution I will also post it here.
Robbie