2013-05-13 87 views
2

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

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

我的问题是通话

gci c:\ -Directory | MyScript 

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

由于提前, 克里斯托夫

回答

4

你需要用你的代码执行到程序块:

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

    PROCESS { 
     $PsPath 
    } 
} 

gci c:\ -Directory | MyScript 

唐·琼斯有BEGIN,PROCESS,& END块的一个很好的破败这里:http://technet.microsoft.com/en-us/magazine/hh413265.aspx