我试图通过管道将两个参数传递给一个函数,但是它似乎没有按预期工作,并且我正在努力理解原因。
function Test-Pipeline {
[CmdletBinding ()]
Param(
[Parameter(ValueFromPipeline=$true)][String]$Name,
[Parameter(ValueFromPipeline=$true)][String]$Value
)
Write-Host "Name: $Name"
Write-Host "Value: $Value"
}
"Name", "Value" | Test-Pipeline
名称:值
值:值
我尝试运行Trace-Command
命令以查看发生了什么。在第35行,我们可以看到Value
已绑定到$Parameter
。
PowerShell为什么将第二个输入绑定到两个参数?如果可以预料,为什么只在第二个参数而不是第一个出现?
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : BIND arg [] to parameter [Name]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION:
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [] to param [Name] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [] to parameter [Value]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION:
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [] to param [Value] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.String]
DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 : Parameter [Value] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Name] to parameter [Value]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: Name
DEBUG: ParameterBinding Information: 0 : BIND arg [Name] to param [Value] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : Parameter [Name] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Name] to parameter [Name]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: Name
DEBUG: ParameterBinding Information: 0 : BIND arg [Name] to param [Name] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.String]
DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 : Parameter [Name] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Value] to parameter [Name]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: Value
DEBUG: ParameterBinding Information: 0 : BIND arg [Value] to param [Name] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : Parameter [Value] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Value] to parameter [Value]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: Value
DEBUG: ParameterBinding Information: 0 : BIND arg [Value] to param [Value] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Test-Pipeline]
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Write-Host]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Write-Host]
DEBUG: ParameterBinding Information: 0 : BIND REMAININGARGUMENTS cmd line args to param: [Object]
DEBUG: ParameterBinding Information: 0 : BIND arg [System.Collections.Generic.List`1[System.Object]] to parameter [Object]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Object]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [System.Collections.Generic.List`1[System.Object]] to param [Object] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Write-Host]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
Name: Value
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Write-Host]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Write-Host]
DEBUG: ParameterBinding Information: 0 : BIND REMAININGARGUMENTS cmd line args to param: [Object]
DEBUG: ParameterBinding Information: 0 : BIND arg [System.Collections.Generic.List`1[System.Object]] to parameter [Object]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Object]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [System.Collections.Generic.List`1[System.Object]] to param [Object] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Write-Host]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
Value: Value
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
答案 0 :(得分:4)
根据Lee Dailey的评论,您只能让一个参数接受“值”(对于每种值类型,例如字符串,整数等)从管道中输入。您的代码当前正在执行的操作是发送一个字符串值数组,然后通过管道一次处理一个字符串值。
如果要将多个值一起发送到管道中,可以通过将这些值设置为自定义对象的属性来实现,然后可以使用ValueFromPipelineByPropertyName
参数通过管道接受它们。通过匹配与输入参数同名的输入对象的任何属性来工作:
function Test-Pipeline {
[CmdletBinding ()]
Param(
[Parameter(ValueFromPipelineByPropertyName=$true)][String]$Name,
[Parameter(ValueFromPipelineByPropertyName=$true)][String]$Value
)
Write-Host "Name: $Name"
Write-Host "Value: $Value"
}
$MyObject = [pscustomobject]@{
Name = "MyName"
Value = "MyValue"
}
$MyObject | Test-Pipeline
结果:
Name: MyName
Value: MyValue
一种替代但类似的方法是使用ValueFromPipeline
接受输入对象,然后从该对象获取属性值:
function Test-Pipeline {
[CmdletBinding ()]
Param(
[Parameter(ValueFromPipeline=$true)][Object]$InputObject
)
$Name = $InputObject.Name
$Value = $InputObject.Value
Write-Host "Name: $Name"
Write-Host "Value: $Value"
}
$MyObject = [pscustomobject]@{
Name = "MyName"
Value = "MyValue"
}
$MyObject | Test-Pipeline
某些cmdlet将支持这两种方法,因为PowerShell会先尝试按对象类型进行匹配,然后再恢复为按属性名称进行匹配。如果您想了解更多信息,请在此处进行详细说明:https://blogs.technet.microsoft.com/heyscriptingguy/2013/03/25/learn-about-using-powershell-value-binding-by-property-name/
请注意,如果要通过管道成功使用值,则还需要在函数中使用Process { }
块,这将导致一次处理一组对象:
function Test-Pipeline {
[CmdletBinding ()]
Param(
[Parameter(ValueFromPipeline=$true)][Object]$InputObject
)
Process {
$Name = $InputObject.Name
$Value = $InputObject.Value
Write-Host "Name: $Name"
Write-Host "Value: $Value"
}
}
$MyObject = @(
[pscustomobject]@{
Name = "MyName"
Value = "MyValue"
}
[pscustomobject]@{
Name = "MySecondName"
Value = "MySecondValue"
}
)
$MyObject | Test-Pipeline
否则,将仅处理对象集合中的最后一个值。