Reputation: 45105
I have a custom CmdLet written in C# which returns an array of objects. For testing purposes, they're anonymous.
protected override void ProcessRecord()
{
var anonType1 = new { name = "Mikey", description = "Brown" };
var anonType2 = new { name = "Davo", description = "Green" };
List<object> stuff = new List<object>();
stuff.Add(anonType1);
stuff.Add(anonType2);
this.WriteObject(stuff.ToArray());
}
This gives the following output in PS2:
name description
---- -----------
Mikey Brown
Davo Green
If I use Select-Object name I would hope to just exclude the 'description' property and get Mikey and Davo stacked on top of each other, but no! I get:
name
----
Where's my data!?
Thanks
Luke
As a side: does anyone know of a good learning resource for programming CmdLets and working with the internals of PS (rather than PS usage which most books are about)? Ta
UPDATE
Even if I make strong types, new a few up and put them in a PSDataCollection it doesn't work as I expect. My expectations are clearly wrong. How do I correctly output collections of data to the pipeline??
SOLVED
We have to set enumerateCollection = true. Sounds stupid but we weren't using the WriteObject protected method but our own WriteToAvailableOutput which diverts to the debugger from within Visual Studio! So we could not see the extra overload on WriteObject - dhuurr!!
Upvotes: 1
Views: 856
Reputation: 45105
The answer it to use the WriteObject overload with the enumerateCollection parameter set to true.
this.WriteObject(stuff.ToArray(), true);
Stupid me.
Upvotes: 1