ValueFromPipelineByPropertyName 可以将函数转移到下一个位置参数吗?

ValueFromPipelineByPropertyName can I shift function next positional parameters?

考虑到设计为使用 属性 名称的值的函数,第二个函数的第一个参数由管道传递。我可以为第二个函数使用位置参数吗?

示例:

function Get-Customer {
    [CmdletBinding()]
    Param(
        [string]$CustomerName = "*"
    )
    Process {
        # real process iterate on fodlers.
        New-Object PSObject -Property @{
            CustomerName = $CustomerName;
        }
    }
}

function Get-Device {
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipelineByPropertyName)]
        [string]$CustomerName = "*",
        [string]$DeviceName = "*"
    )

    Process {
        # real process iterate on fodlers.
        New-Object PSObject -Property @{
            CustomerName=$CustomerName;
            DeviceName=$DeviceName
        }
    }
}

您可以像这样使用它:

Get-Customer "John" | Get-Device
Get-Customer "John" | Get-Device -DeviceName "Device 1"

但是你能做到吗(实际上提供的代码不起作用)?

Get-Customer "John" | Get-Device "Device 1"

您需要将 $DeviceName 定义为第一个位置参数才能起作用:

function Get-Device {
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipelineByPropertyName)]
        [string]$CustomerName = "*",

        [Parameter(Position=0)]
        [string]$DeviceName = "*"
    )

    Process {
        # real process iterate on fodlers.
        New-Object PSObject -Property @{
            CustomerName = $CustomerName;
            DeviceName   = $DeviceName
        }
    }
}