0

我想将 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

4

1 回答 1

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

微软的文档

于 2013-06-21T18:31:00.507 回答