Reputation: 1781
I'll use Test-Path
as the example here.
You can pass the array as parameters to the function:
Test-Path 'C:\', 'G:\
Or they can be piped:
'C:\', 'G:\ | Test-Path
Now this is simple enough as it's just an array of strings.
But what if I wanted to do that with objects?
Again, I'm skimping on the details here. But I have a function that has 3 arguments:
If I'm calling the function with only one object. All of these invocations are valid.
My-Function -RequiredString 'requiredstring' -OptionalString 'optionalstring' -MySwitch
My-Function -RequiredString 'requiredstring'
My-Function -RequiredString 'requiredstring' -MySwitch
If I want to process multiple objects at once. I do something like this:
$objs = @(
(New-Object PSObject -Property @{ RequiredString='requiredstring'; OptionalString='optionalstring'; MySwitch=$true} ),
(New-Object PSObject -Property @{ RequiredString='requiredstring'} ),
(New-Object PSObject -Property @{ RequiredString='requiredstring'; MySwitch=$true} )
)
$objs | My-Function
Is there any way that I can call the function to process multiple objects without using a pipeline?
Upvotes: 0
Views: 70
Reputation: 68331
No.
If you want to pass an array of objects to the function, the function will need to be re-written to expect and iterate through the array.
The pipeline is doing that for you, by "unrolling" the array and passing one object at a time to the function. If you want to pass the array as a parameter, then that unrolling/iteration will need to be done inside the function.
Upvotes: 1