格式化为表格对象数组

时间:2013-06-20 13:36:37

标签: powershell powercli

我想格式化为一个PSObject数组,我的代码是:

$object = @()
Foreach ($Alarm in Get-AlarmDefinition) {
    Foreach ($AlarmAction in Get-AlarmAction -AlarmDefinition $Alarm) {
        $obj = New-Object PSObject -property  @{Definition = $Alarm.Name; Action =""; GY=""; YR=""; RY=""; YG=""}
        Foreach ($AlarmActionTrigger in Get-AlarmActionTrigger -AlarmAction $AlarmAction) {
            $obj.Action = $AlarmAction.ActionType
            If ($AlarmActionTrigger.StartStatus -eq "Green") {
                $obj.GY = $AlarmActionTrigger.Repeat
            } Else {
                If($AlarmActionTrigger.StartStatus -eq "Red") {
                    $obj.RY = $AlarmActionTrigger.Repeat
                } Else {
                    If ($AlarmActionTrigger.EndStatus -eq "Green") {
                        $obj.YG = $AlarmActionTrigger.Repeat
                    } Else {
                        $obj.YR = $AlarmActionTrigger.Repeat
                    }
                }
            }
        }
        $object += $obj
    }
}
$object | Format-Table Definition, Action,GY,YR,RY,YG -auto

但是会返回此错误:

  

ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

有人可以帮忙吗? TNX

1 个答案:

答案 0 :(得分:4)

在调用“Format-Table”

之前,您可能想尝试设置表的格式

像这样:

$myformat = @{Expression={$_.*one*};Label="*name*";width=10},   
@{Expression={$_.*two*};Label="*Two*";width=50},  

$Result = $object | Format-Table $myformat -Wrap | Out-String  
Write-Host $Result

Microsoft's Documentation