2013-05-13 89 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