Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 飞溅不';不能使用参数过滤器_Powershell_Parameter Splatting - Fatal编程技术网

Powershell 飞溅不';不能使用参数过滤器

Powershell 飞溅不';不能使用参数过滤器,powershell,parameter-splatting,Powershell,Parameter Splatting,我有一条很长的线,我想让它更容易阅读: $Mail = "stantastic@example.com" Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress 我读到使用Splating很好,所以我尝试: $Params =

我有一条很长的线,我想让它更容易阅读:

$Mail = "stantastic@example.com"    
Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress 
我读到使用Splating很好,所以我尝试:

$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 Get ADUser:分析查询时出错:'{EmailAddress-eqstantastic@example.com}错误消息:“语法错误”位于位置:“1”。 第1行字符:1 +获取ADUser@Params + ~~~~~~~~~~~~~~~~~~ +CategoryInfo:ParserError:(:)[Get ADUser],ADFilterParsingException +FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
我缺少什么?

您应该将筛选器作为
字符串传递给
ActiveDirectory
模块cmdlet。代码中不必要地包含括号:

Get-ADUser -Filter "EmailAddress -eq '$Mail'"
虽然您可以传递
脚本块
,但无论如何,它会隐式地转换为
字符串
。此外,当属性需要
字符串的
数组时,您将属性作为单个
字符串传递


正确的方法是:

$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>]
Get ADUser-过滤器
[-ResultPageSize]
[-ResultSetSize]
[-SearchBase]
[-SearchScope{Base | OneLevel | Subtree}]
[-AuthType{Negotiate | Basic}]
[-凭证]
[-分区]
[-财产]
[-服务器]
[]