Powershell 使用括号将变量传递给其他变量

时间:2021-05-20 18:32:27

标签: powershell exchange-server

对不起,标题令人困惑,但这是我可以描述它而不在标题中发布代码的最佳方式。我尝试将 $MajorCode 变量放入 $filter 变量中。这样输出看起来像

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

CSV 如下所示:

Name             Alias       EmailAddress                  code 
DDL-PayPal       PayPal    PayPal0@test.test               2101 

完整代码如下:

$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
 }

1 个答案:

答案 0 :(得分:0)

继续我的评论,RecipientFilter 应该是一个字符串,特别是如果它包含一个变量,而不是一个脚本块

试试

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

在整个字符串周围使用双引号,以便扩展里面的变量。在里面,使用单引号