3

我试图通过管道将两个参数传递给一个函数,但它似乎没有按预期工作,我很难理解为什么。

MWE

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
4

2 回答 2

4

根据 Lee Dailey 的评论,您只能让一个参数通过“值”接受来自管道的输入(对于每种值类型,例如 string、int 等)。您的代码当前正在做的是发送一个字符串值数组,然后通过管道一次处理一个。

如果您想将多个值一起发送到管道,您可以通过使这些值成为自定义对象的属性来实现,然后您可以使用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-属性名称/

请注意,如果您要通过管道成功处理值,您还需要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

如果没有这个,只会处理对象集合中的最后一个值。

于 2018-12-14T13:24:51.533 回答
1

除了@Mark 的回答,如前所述,每个参数类型只能设置一次 ValueFromPipeline 属性。
这是使用多个 ValueFromPipeline 的示例,每个用于不同的参数类型。

每次迭代只分配一个参数。匹配输入值类型的那个。

注意:
$Int 包含0在初始化时没有使用特定值的情况。
检查使用了哪个参数集更安全,而不仅仅是分配了值。

function testus() {
  param(
    [Parameter(ValueFromPipeline, ParameterSetName = "A")]
    [String]$Str,
    [Parameter(ValueFromPipeline, ParameterSetName = "B")]
    [Int]$Int
  )
  process {
    if ($PSCmdlet.ParameterSetName -eq 'A') {
        Write-Host ""
        Write-Host "String was used:"
        Write-Host "Str: $Str"
        Write-Host "(Int contains initialized default value '0': $Int)"
    } elseif ($PSCmdlet.ParameterSetName -eq 'B') {
        Write-Host ""
        Write-Host "Integer was used:"
        Write-Host "Int: $Int"
        Write-Host "(Str contains initialized default value '' (empty string): $($Str.gettype().FullName))"
    }
  }
}

'a', 1, 0 | testus
于 2021-12-11T20:18:41.253 回答