format-table和属性是数组

时间:2012-07-27 20:29:09

标签: powershell

我使用这个powershell命令:

get-vm | ft name, *start*, *stop*, customproperties

返回带有字符串数组作为属性的对象(customproperties):

Name                StartAction DelayStart      StopAction CustomProperties
----                ----------- ----------      ---------- ----------------
TKAD4        AlwaysAutoTurnOnVM          0 ShutdownGuestOS {NoStartupDelay, ...
TKAD3        AlwaysAutoTurnOnVM          0 ShutdownGuestOS {NoStartupDelay, ...

我怎样才能从数组中返回一个元素?作为对象的属性是否将它作为表的一部分显示?

我想要的输出看起来像这样:

Name                StartAction DelayStart      StopAction        Custom1
----                ----------- ----------      ----------        -------
TKAD4        AlwaysAutoTurnOnVM          0 ShutdownGuestOS NoStartupDelay
TKAD3        AlwaysAutoTurnOnVM          0 ShutdownGuestOS NoStartupDelay

1 个答案:

答案 0 :(得分:4)

在您的格式表中,将customproperties更改为:

@{label='Custom1';e={$_.CustomProperties[0]}}

如果是阵列。如果是集合使用:

@{label='Custom1';e={$_.CustomProperties | Select -First 1}}