我有一个用C#编写的自定义CmdLet,它返回一个对象数组。出于测试目的,他们是匿名的。
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());
}
这在PS2中给出了以下输出:
name description
---- -----------
Mikey Brown
Davo Green
如果我使用选择对象名称,我希望只是排除'description'属性并将Mikey和Davo叠加在一起,但不是!我明白了:
name
----
我的数据在哪里??
由于
路
作为一方:有没有人知道编程CmdLets和使用PS内部的良好学习资源(而不是大多数书籍的PS使用情况)? TA
更新
即使我制作了强大的类型,新的一些并将它们放在PSDataCollection中,它也不能像我期望的那样工作。我的期望显然是错误的。如何正确输出数据集合到管道?
解决
我们必须设置enumerateCollection = true。听起来很愚蠢,但我们没有使用WriteObject保护方法,而是使用我们自己的WriteToAvailableOutput,它从Visual Studio中转移到调试器!所以我们看不到WriteObject上的额外重载 - dhuurr !!
答案 0 :(得分:1)
使用WriteObject重载并将enumerateCollection参数设置为true的答案。
this.WriteObject(stuff.ToArray(), true);
愚蠢的我。