【问题标题】:Format as Table an Array of Object将对象数组格式化为表格
【发布时间】:2013-06-20 13:36:37
【问题描述】:

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

【问题讨论】:

  • 将管道移至格式表后会得到什么?

标签: powershell powercli


【解决方案1】:

您可能想在调用“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

【讨论】:

  • Tnx!!!它也是这样工作的(没有格式定义):$object |格式-表定义,动作,GY,YR,RY,YG -auto |外串
猜你喜欢
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2018-07-18
  • 2020-10-19
  • 1970-01-01
相关资源
最近更新 更多