3

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.