2

我有这么长的一行,我想让它更容易阅读:

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

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

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

Get-ADUser @Params

但是运行它会引发错误:

Get-ADUser:解析查询时出错:“{ EmailAddress -eq stantastic@example.com }”错误消息:“语法错误”在位置:“1”。
在行:1 字符:1
+ 获取 ADUser @Params
+~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我错过了什么?

4

1 回答 1

5

您应该将过滤器ActiveDirectory作为string. 您在代码中不必要地包含括号:

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

虽然您可以通过 a ,但无论如何scriptblock它都隐含地变成了 a 。此外,当您期望of时string,您将属性作为单个传递。stringarraystring


正确的方法:

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

我建议检查Get-Help参数类型:

Get-ADUser -Filter <string>
[-ResultPageSize <int>]
[-ResultSetSize <System.Nullable[System.Int32]>]
[-SearchBase <string>]
[-SearchScope {Base | OneLevel | Subtree}]
[-AuthType {Negotiate | Basic}]
[-Credential <PSCredential>]
[-Partition <string>]
[-Properties <string[]>]
[-Server <string>]
[<CommonParameters>]
于 2019-03-13T15:29:26.767 回答