我有一个命令,可以通过代理电子邮件地址搜索用户。
Get-ADUser -Filter {ProxyAddresses -like "*bob@bob.com*"}
此命令返回我想要的用户的记录,它可以正常工作。
我还可以创建一个变量来存储将要找到的确切字符串并搜索它,我也得到了正确的结果。
$email = "smtp:bob@bob.com"
Get-ADUser -Filter {ProxyAddresses -like $email}
我不想在我正在搜索的记录不是smtp的情况下执行上述操作:(有些可能是sip或其他人)。
但是,我正在迭代用户列表,所以我需要查找$ email而不是实际的字符串。我想这样做:
Get-ADUser -Filter {ProxyAddresses -like "*$email*"}
返回$ null。我无法弄清楚为什么。我已经打开了一个新窗口并将$ email设置为相当于我知道自己键入的地址,但结果仍为$ null。
我认为这与添加通配符有关,但我无法弄清楚问题是什么。
也许这是引号,因为以下内容也失败了:
Get-ADUser -Filter {ProxyAddresses -like "$email"}
我之前总是能够在字符串中使用变量,所以我不明白为什么它会失败。
答案 0 :(得分:3)
My usual recommendation is to use -LDAPFilter
rather than -Filter
.
Get-ADUser -LDAPFilter "(proxyAddresses=*$email*)"
Internally, Get-ADUser must translate the -Filter
to an LDAP filter anyway, and as you have seen, getting -Filter
to work with embedded variables can be tricky.
答案 1 :(得分:0)
I think it wont work, because you use $email inside of {}
.
Inside the function term, you can't use variables from outside. You have to put it into the global scope or access it with having.
So try this one:
Get-ADUser -Filter {ProxyAddresses -like "*$having:email*"}
or set $email to global:
$global:email = $email
Get-ADUser -Filter {ProxyAddresses -like "*$global:email*"}
Just have a look at the Scopes