我在管道中有一些对象,如下所示:
$arr1 = @(
(New-Object -TypeName psobject -Property @{'objname'='obj1';props=@((new-object -typename psobject -Property @{'pname'='prop1';'val'=11;}),(new-object -typename psobject -Property @{'pname'='prop2';'val'='xx';}))}),
(New-Object -TypeName psobject -Property @{'objname'='obj2';props=@((new-object -typename psobject -Property @{'pname'='prop2';'val'='yy';}),(new-object -typename psobject -Property @{'pname'='prop3';'val'=22;}))})
)
$arr1 | select objname, props
结果:
objname props
------- -----
obj1 {@{val=11; pname=prop1}, @{val=xx; pname=prop2}}
obj2 {@{val=yy; pname=prop2}, @{val=22; pname=prop3}}
我需要展开道具并在对象名称旁边列出每个展开的属性。没有更简单的:
$arr1 | select objname -ExpandProperty props
结果:
val pname objname
--- ----- -------
11 prop1 obj1
xx prop2 obj1
yy prop2 obj2
22 prop3 obj2
到目前为止,一切都很好。当管道中的某些对象没有道具时,问题就开始了:
$arr2 = @(
(New-Object -TypeName psobject -Property @{'objname'='obj1';props=@((new-object -typename psobject -Property @{'pname'='prop1';'val'=11;}),(new-object -typename psobject -Property @{'pname'='prop2';'val'='xx';}))}),
(New-Object -TypeName psobject -Property @{'objname'='obj2';props=@()})
)
$arr2 | select objname,props
结果:
objname props
------- -----
obj1 {@{val=11; pname=prop1}, @{val=xx; pname=prop2}}
obj2 {}
现在我希望,该命令:
$arr2 | select objname -ExpandProperty props
会返回这样的结果(obj2 没有额外的道具,但它仍在管道中,它的“默认”属性 objname):
val pname objname
--- ----- -------
11 prop1 obj1
xx prop2 obj1
obj2
相反,缺少 obj2:
val pname objname
--- ----- -------
11 prop1 obj1
xx prop2 obj1
如果没有可扩展的内容,为什么 ExpandProperty 不会留下我的对象?