ValueFromPipelineByPropertyName可以移动函数下一个位置参数吗?

时间:2018-01-26 19:15:10

标签: powershell pipeline cmdlets

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

示例:

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

您可以像以下一样使用它:

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

但你能做到这一点(实际上提供的代码并不起作用)?

SELECT * FROM A

LEFT JOIN B
ON B.id = A.id

LEFT JOIN C
ON C.other = A.other

WHERE

(SELECT SUM(C.quantity) FROM C WHERE C.other = A.other) < A.quantity

1 个答案:

答案 0 :(得分:0)

您需要将$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
        }
    }
}