Author Posts

August 19, 2018 at 2:42 pm

My question is of  general nature.  Is there a way to determine if a cmdlet is expecting a collection or array in accepting pipeline input?

I understand the general "goto" is the -InputObject parameter if it exists specific to a cmdlet, but what about cmdlets that do not have this parameter?

Would be grateful for any advice, tips, references.

 

August 19, 2018 at 3:00 pm

Input datatype for a parameter is same even if it accepts input via pipeline.

You can check the input datatype and more for all parameters in a cmdlet using

Get-Help Get-Process -Full

If your target is specific to parameter, you can get it via

Get-Help Get-Process -Parameter Name

August 19, 2018 at 8:32 pm

Ignoring pipeline input for a moment, you'll see it in the help:

-Name [string[]]

The [] indicates an array is required. If you pass a single value, PowerShell will create a single-item array from it.

But that doesn't cover pipeline input; pipeline input is always implicitly single items. That is, the pipeline conveys one item at a time from command to command. That allows multiple commands to essentially run simultaneously. Ergo, any parameter capable of accepting pipeline input can "kind of" accept multiple values, but they do it one at a time, not in one big chunk.

Help files clearly indicate which parameters accept pipeline input.

August 20, 2018 at 10:52 am

Thank you Mr Jones, much appreciated.

August 20, 2018 at 12:59 pm

Cmdlets have process blocks that automatically run for every element in an array, as long as the parameter accepts pipeline input. To accept an array directly as a parameter, you would need an extra foreach loop.

# can take an array over the pipe, but not as a parameter, very common
function hi { 
  param([Parameter(ValueFromPipeline=$True)]$inputvalue)
  process { 
    $inputvalue.gettype()
  } 
}

PS C:\Users\js> 1,2,3 | hi

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType
True     True     Int32                                    System.ValueType
True     True     Int32                                    System.ValueType


# can take an array as a parameter too, less common
function hi2 { 
  param([Parameter(ValueFromPipeline=$True)]$inputvalue)
  process { 
    foreach ($value in $inputvalue) {
      $value.gettype()
    }
  } 
}

PS C:\Users\js> hi2 -inputvalue 1,2,3

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType
True     True     Int32                                    System.ValueType
True     True     Int32                                    System.ValueType

August 24, 2018 at 3:05 pm

Many thanks Mr JS, much appreciated.

Your two examples have indeed clarified many of my wrong impressions with pipeline processing.

Best,

August 24, 2018 at 3:22 pm

Thanks. Sometimes I get ghosted after answering, lol.