Christoph
Christoph

Reputation: 2004

Pass directories from pipeline as Powershell named parameter

I try to write a Powershell script that accepts directories from the pipeline as named parameter. My parameter declaration looks like

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

My problem is that the call

gci c:\ -Directory | MyScript

results in only the last element of the result of gci being in the input array. What is wrong here?

Thanks in advance, Christoph

Upvotes: 3

Views: 297

Answers (1)

xcud
xcud

Reputation: 14722

You need to wrap up your execution code into a PROCESS block:

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

    PROCESS {
        $PsPath
    }
}

gci c:\ -Directory | MyScript

Don Jones has a nice rundown of the BEGIN, PROCESS, & END blocks here: http://technet.microsoft.com/en-us/magazine/hh413265.aspx

Upvotes: 5

Related Questions