首页 > 解决方案 > Get-AzureADUser 过滤器 - “没有输入就无法评估脚本块”

问题描述

有人可以向我解释为什么以下工作:

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

谢谢。

标签: powershell

解决方案


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

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

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

请参阅Get-ADUser-Filter以查看有关Get-ADUser.

请参阅Get-AzureADUser以查看有关该Get-AzureADUser -Filter参数的更多信息。

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


推荐阅读