Author Posts

January 11, 2016 at 4:39 pm

I have a script I'm writing that works great when the data is piped in,

gc .\Desktop\PCs.txt |Repair-WinAgent

but when I run it with an array in the parameter, it attempts to process all values in the array as one big long string.

Repair-WinAgent -ComputerName (gc .\Desktop\PCs.txt) 

here's my param section.

        # Hostname(s) or IP Address(es) of the system(s) with the agent issue.  Can be piped in as a [string[]] value. 
        $ComputerName = 'localhost'

January 11, 2016 at 6:42 pm

Depends on what's in your function's Process block. Since $ComputerName is an array, you'll likely need a loop of some sort to enumerate that array. When you pipe stuff in, the process block gets called multiple times with just one string each time, but when you pass in the array on the command line, the process block gets called only once, and $ComputerName will contain the whole list of names.

January 12, 2016 at 7:44 am

Thanks Dave,
Process has a bunch of if statements and whatnots that all reference the variable as if it were a single value.
So if I wrap the whole process block (contents) inside a foreach loop, i should be all set for both cases, right?

January 12, 2016 at 8:04 am

Yep, exactly. Here's the basic skeleton I use for this type of function:

function Do-Something
    param (
        [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string[]] $ComputerName = 'localhost'

        foreach ($name in $ComputerName)
            # Do stuff with $name