Splatting 不适用于参数 -Filter
Splatting doesn't work with parameter -Filter
我有这么长的一行,我想让它更容易阅读:
$Mail = "stantastic@example.com"
Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress
我听说使用 splatting 很不错,所以我正在尝试:
$Params = @{
Server = 'example.com:3268'
Filter = '{ EmailAddress -eq $Mail }'
Properties = 'CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress'
}
Get-ADUser @Params
但是 运行 这会引发错误:
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
我错过了什么?
您应该将过滤器作为 string
传递给 ActiveDirectory
模块 cmdlet。您在代码中不必要地包含括号:
Get-ADUser -Filter "EmailAddress -eq '$Mail'"
虽然您 可以 传递 scriptblock
,但无论如何它都会隐式地变成 string
。此外,当期望 array
为 string
.
时,您将属性作为单个 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
参数类型:
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>]
我有这么长的一行,我想让它更容易阅读:
$Mail = "stantastic@example.com"
Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress
我听说使用 splatting 很不错,所以我正在尝试:
$Params = @{
Server = 'example.com:3268'
Filter = '{ EmailAddress -eq $Mail }'
Properties = 'CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress'
}
Get-ADUser @Params
但是 运行 这会引发错误:
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
我错过了什么?
您应该将过滤器作为 string
传递给 ActiveDirectory
模块 cmdlet。您在代码中不必要地包含括号:
Get-ADUser -Filter "EmailAddress -eq '$Mail'"
虽然您 可以 传递 scriptblock
,但无论如何它都会隐式地变成 string
。此外,当期望 array
为 string
.
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
参数类型:
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>]