将目录作为Powershell命名参数从管道中传递

将目录作为Powershell命名参数从管道中传递,powershell,parameters,pipeline,Powershell,Parameters,Pipeline,我尝试编写一个Powershell脚本,它接受来自管道的目录作为命名参数。我的参数声明如下所示 param([Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)] [System.IO.DirectoryInfo[]] $PsPath) 我的问题是电话 gci c:\ -Directory | MyScript 结果只有gci结果的最后一个元素在输入数组中。这里怎

我尝试编写一个Powershell脚本,它接受来自管道的目录作为命名参数。我的参数声明如下所示

param([Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)] [System.IO.DirectoryInfo[]] $PsPath)
我的问题是电话

gci c:\ -Directory | MyScript
结果只有
gci
结果的最后一个元素在输入数组中。这里怎么了

提前感谢,,
Christoph

您需要将执行代码包装到进程块中:

function MyScript {
    param(
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true, 
                   ValueFromPipelinebyPropertyName=$true)] 
        [System.IO.DirectoryInfo[]] $PsPath
    )

    PROCESS {
        $PsPath
    }
}

gci c:\ -Directory | MyScript
Don Jones在这里详细介绍了开始、过程和结束块: