Author Posts

November 2, 2015 at 7:43 am

I have created a function that has a parameter ComputerName. I use ValueFromPipline=$true, and everything works OK. So I can use the function with a single value or a series of values from the pipeline.
How can I also use a list of values, similar to most CmdLets that have a ComputerName parameter.
I what the function to accept a single value, a list of values or accept values from the pipeline.
I just can't workout the list of values part.

Thanks

November 2, 2015 at 8:09 am

This is the usual setup for parameters that fit your description:

function Test-Something
{
    param (
        [Parameter(ValueFromPipeline)]
        [string[]] $ComputerName = '.'
    )

    process
    {
        foreach ($computer in $ComputerName)
        {
            "Processing $computer"
        }
    }
}

By declaring the parameter as an array (in this case, [string[]]), callers can then use any of these conventions:

Test-Something -ComputerName 'Single'
Test-Something -ComputerName 'Multiple 1', 'Multiple 2'
'Multiple 1', 'Multiple 2' | Test-Something

November 2, 2015 at 8:35 am

Brilliant! Thank You.