3

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

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

我的问题是电话

gci c:\ -Directory | MyScript

gci仅导致输入数组中结果的最后一个元素。这里有什么问题?

在此先感谢,克里斯托夫

4

1 回答 1

5

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

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

    PROCESS {
        $PsPath
    }
}

gci c:\ -Directory | MyScript

Don Jones 在此处对 BEGIN、PROCESS 和 END 块进行了很好的概述:http ://technet.microsoft.com/en-us/magazine/hh413265.aspx

于 2013-05-13T12:54:55.737 回答