喷溅不适用于参数-Filter

时间:2019-03-13 15:21:05

标签: powershell parameter-splatting

我想让这行很长,以便阅读:

$Params = @{
    Server = 'example.com:3268'
    Filter = '{ EmailAddress -eq $Mail }'
    Properties = 'CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress'
}

Get-ADUser @Params

我读到使用喷溅效果很好,所以我正在尝试:

Ende

但是运行它会引发错误:

Get-ADUser : Error parsing query: '{ EmailAddress -eq stantastic@example.com }' Error Message: 'syntax error' at position: '1'.
At line:1 char:1
+ Get-ADUser @Params
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我想念什么?

1 个答案:

答案 0 :(得分:4)

您应该将过滤器作为ActiveDirectory传递到string模块cmdlet。您无需在代码中加入方括号:

Get-ADUser -Filter "EmailAddress -eq '$Mail'"

尽管您可以通过scriptblock,但无论如何它都隐式地变成了string。此外,当期望<{1}}的 string 时,您将作为单个array传递属性。


正确的方法:

string

我建议检查$aduserParams = @{ Server = 'example.com:3268' Filter = "EmailAddress -eq '$Mail'" Properties = 'CN', 'co', 'Company', 'Department', 'DisplayName', 'SamAccountName', 'State', 'Office', 'EmailAddress' } Get-ADUser @aduserParams 的参数类型:

Get-Help