-
Website
http://HuddledMasses.org/ -
Original page
http://HuddledMasses.org/writing-cmdlets-for-the-powershell-pipeline/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
Clint Ecker
1 comment · 5 points
-
avenzke
1 comment · 1 points
-
Euri
1 comment · 4 points
-
Stuart
1 comment · 1 points
-
Luciano Evaristo Guerche
1 comment · 1 points
-
-
Popular Threads
You know, I've looked at your articles about cmdlets/functions in the pipeline and I feel you're missing something. The purpose of the InputObject parameter is to pass in a collection as a single object. This is as opposed to using the pipeline where a collection is passed along the pipeline one item at a time. There are cases where you want to pass in a collection as a collection.
In this particular article, you mention how certain cmdlets don't actually work when you use the InputObject parameter. But if you look at your example (Select -First 3 -Unique -InputObject $a), this does in fact work. It receives one object, an array. It then selects the first 3 objects, but there is only 1 so that is moot. And lastly it selects unique objects, but again there is only 1 so that is moot as well and finally the object is output using the default formatter. In this case the default formatter is showing the contents of the array. Look at these two examples:
Select -First 3 -Unique -InputObject $a | % { $_.ToString() }
$a | Select -First 3 -Unique | % { $_.ToString() }
In the first case, the output is System.Object[]. which is correct because that is the default string conversion of a collection of objects. In the second case you see the same output as if you didn't put the foreach-object cmdlet in the pipeline, which is also correct because the items in the collection are strings and therefore when you call ToString() on them you get them output as strings.
My point in all of this is that InputObject is actually a very useful parameter, because there are cases where you really want to pass a collection as a collection into a cmdlet and then do something with it. By making InputObject instead split the collection passed in and pipeline it through, you're forcing users to wrap collections in an array just to get them passed in as a collection, and personally I don't feel they should have to do that.
The only thing that I can see wrong with InputObject is that it isn't documented clearly enough.
I'd like to hear your thoughts on this, either here or via email because I've been working on cmdlets/functions that work in the pipeline lately and I have tried to follow the PowerShell core model of the InputObject parameter. I'd be interested in hearing arguments about how InputObject is broken so that I can make sure I'm making the right choice when it comes to that parameter.
--
Kirk Munro
Poshoholic
http://poshoholic.com
Quite simply, I disagree. The documentation for these parameters says quite clearly that inputObject “Specifies an object or objects to input to the cmdlet.†This clearly means that I should be able to pass multiple objects, *and have them treated as multiple objects*, not as a single array object. ... "More over here":http://huddledmasses.org/whats-the-desired-behavior-of-inputobject/
-inputobject $array[0]
SS