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

Chr*_*oph 3 parameters powershell pipeline

我尝试编写一个Powershell脚本,它接受来自管道的目录作为命名参数.我的参数声明看起来像

param([Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)] [System.IO.DirectoryInfo[]] $PsPath)
Run Code Online (Sandbox Code Playgroud)

我的问题是电话

gci c:\ -Directory | MyScript
Run Code Online (Sandbox Code Playgroud)

只导致gci输入数组中结果的最后一个元素.这有什么不对?

先谢谢,Christoph

xcu*_*cud 5

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

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

    PROCESS {
        $PsPath
    }
}

gci c:\ -Directory | MyScript
Run Code Online (Sandbox Code Playgroud)

Don Jones在这里有一个BEGIN,PROCESS和END块的简要概述:http://technet.microsoft.com/en-us/magazine/hh413265.aspx