Search code examples
powershellexchange-server

Powershell passing variables into other variables with parentheses


Sorry for the confusing title but that is the best way I could describe it without posting the code in the title. I am try to take the $MajorCode variable and get it into the $filter variable. so that the output looks like

{((customattribute5 -eq "CurrentStudent") -AND (customattribute3 -eq "2101"))}
                NOT
{((customattribute5 -eq "CurrentStudent") -AND (customattribute3 -eq $MajorCode))}

the CSV looks like this:

Name             Alias       EmailAddress                  code 
DDL-PayPal       PayPal    [email protected]               2101 

Full code is this:

$groups = Import-Csv -Path .\Book2.csv

foreach
($group in $groups)
{

$MajorCode = $group.code

$Filter = {{((customattribute5 -eq "CurrentStudent") -AND (customattribute3 -eq $MajorCode))}}

New-DynamicDistributionGroup -Name $group.Name -Alias $group.Alias -RecipientFilter $filter
 }

Solution

  • Continuing from my comment, the RecipientFilter should be a string, especially if it contains a variable, not a scriptblock

    Try

    $Filter = "(customattribute5 -eq 'CurrentStudent') -AND (customattribute3 -eq '$MajorCode'")
    

    Use double-quotes around the whole string so the variable inside gets expanded. Inside, use single quotes