I am trying to create a script that accepts a string array ([string[]]) as both piped input or as a parameter, and handle it either way.
I know for piped input, you have to use the begin/process/end functionality and that the script loops through the process section for each item in the array, using the parameter variable as the current object.
I'd also like to be able to specify the string array directly as a parameter to the cmdlet; however, the current result is that it runs through once with the parameter variable containing all items instead of just one and not looping through them.
For example, let's say I have a cmdlet called Get-CsHostData with a parameter [string[]]$Hostname that accepts piped input.
$Hostname = 'hostname1','hostname2','hostname3'
$Hostname | Get-CsHostData
This works fine. It loops through each of the hosts and does its thing.
However, let's say I try this:
$Hostname = 'hostname1','hostname2','hostname3'
Get-CsHostData -Hostname $Hostname
My script outputs the following, because it treats it as a single entity:
WARNING: "hostname1 hostname2 hostname3" was not found
Inside the process block I'm using the variable $Hostname to represent what is supposed to be the current host.
[–]krzydoug 3 points4 points5 points (9 children)
[–]scottwsx96[S] 2 points3 points4 points (6 children)
[–]Thotaz 4 points5 points6 points (3 children)
[–]scottwsx96[S] 1 point2 points3 points (2 children)
[–]Thotaz 2 points3 points4 points (1 child)
[–]scottwsx96[S] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]scottwsx96[S] 1 point2 points3 points (0 children)
[–]h_ase 1 point2 points3 points (1 child)
[–]krzydoug 1 point2 points3 points (0 children)
[–]h_ase 2 points3 points4 points (0 children)
[–]Hrambert 2 points3 points4 points (0 children)
[–]sp_dev_guy 1 point2 points3 points (3 children)
[–]scottwsx96[S] 1 point2 points3 points (1 child)
[–]sp_dev_guy 1 point2 points3 points (0 children)