Get-AzureADUser过滤器-“没有输入就无法评估脚本块”

时间:2019-05-24 09:14:48

标签: powershell

有人可以向我解释以下原因的原因吗?

$email = 'fred@bloggs.com'
Get-ADUser -Filter {mail -eq $email}

但是当我这样做时:

$email = "fred@bloggs.com" 
Get-AzureADUser -Filter {mail -eq $email}

我得到:

Get-AzureADUser : Cannot evaluate parameter 'Filter' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.

谢谢。

1 个答案:

答案 0 :(得分:1)

这两个命令使用两种不同类型的过滤器。

Get-ADUser使用PowerShell表达式语言语法。它可以接受周围的{},即使在技术上不应该使用它们,因为它实际上不是脚本块。这也意味着您可以使用PowerShell操作符的子集,例如-eq-like等。此过滤器的正确语法是"mail -eq '$email'"。需要用引号引起来,因为PowerShell将双引号内的字符串扩展到传递给Get-ADUser之前,这将导致mail -eq user@domain.com(注意电子邮件地址周围没有引号)并抛出错误

Get-AzureADUser使用oData v3.0过滤器语句。该规范不允许使用PowerShell运算符语法,因为它具有自己的规则。现在,它还允许使用脚本块({})语法。构造此过滤器的正确方法是-Filter "mail eq '$email'"。注意,它使用eq而不是-eq。使用oData过滤器,您可以访问使数据检索和操作更加容易的功能。 Get-AzureADUser -Filter "startswith(Mail,'$email')"是使用函数的一个示例。

有关-Filter的{​​{1}}参数的信息,请参见Get-ADUser

请参见Get-AzureADUser,以查看有关Get-ADUser参数的更多信息。

附加链接oData Filter Querying Collections包含一个可接受的运算符和函数表,以添加重要的查询功能。