0

I'm having a problem comparing a variable.

Can someone please enlighten me as to what I am doing wrong?

"account.name" is the SamAccountName in this example.

$DN = (Get-Mailbox account.name).DistinguishedName

$DN

Returns: CN=Account.Name,OU=Users,OU=IT,OU=Science Lab,DC=company,DC=com

$DN.GetType().Name

Returns: String

Get-DistributionGroup -ResultSize Unlimited -Filter {Members -eq "$DN"}

Returns: nothing

Get-DistributionGroup -ResultSize Unlimited -Filter {Members -eq "CN=Account.Name,OU=Users,OU=IT,OU=Science Lab,DC=company,DC=com"}

Returns: correct results

0

The problem is the -Filter parameter uses a scriptblock. Try it this way:

$scriptBlock = [scriptblock]::Create("Members -eq ""$DN""")
Get-DistributionGroup -ResultSize Unlimited -Filter $scriptBlock
  • Thanks Bill ! That works! And I just applied it to a similar situation: This wasn't working: ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited -Filter {RecipientTypeDetails -eq "SharedMailbox"})) { Get-ADUser -Filter {UserPrincipalName -eq "$Mailbox.UserPrincipalName" } So I used your advice and did it this way: ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited -Filter {RecipientTypeDetails -eq "SharedMailbox"})) { $ScriptBlock = [scriptblock]::Create("UserPrincipalName -eq ""$($Mailbox.UserPrincipalName)""") Get-ADUser -Filter $ScriptBlock } – Wrong Species Feb 16 '17 at 19:08
0

Remove the quotes.

Get-DistributionGroup -ResultSize Unlimited -Filter {Members -eq $DN}

Will do it.

The reason it returns nothing is because with the quotes its looking specifically for $DN, rather than the data that variable contains.

  • Removing the quotes doesn't work. The variable will actually be evaluated as $null. See my answer for a workaround. – Bill_Stewart Feb 15 '17 at 22:28
0

Found another way to do this, using "quotes" instead of {scriptblock}: Get-DistributionGroup -ResultSize Unlimited -Filter "Members -eq '$DN'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.