【问题标题】:Powershell script error when output Array inside if statement在if语句中输出数组时出现Powershell脚本错误
【发布时间】:2016-10-13 16:21:36
【问题描述】:

我在 powershell 脚本中运行以下脚本并出现错误。

$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue

$list = $queues | ft - 属性名称,MessagesInQueue

for ( $i = 0; $i -lt 6; $i++ ) {

如果 ($i -gt 2) {

$list[$i] 

}

}

错误: out-lineoutput :“Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData”类型的对象无效或顺序不正确。这很可能是由一个 与默认格式冲突的用户指定的“format-*”命令。 + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

【问题讨论】:

    标签: powershell


    【解决方案1】:

    当您使用Format-Table(或您使用的简称为FT)显示时,您似乎只是想跳过数据的标题。为此,只需使用 FT 命令上的-HideTableHeaders 开关,不要将其捕获到变量中。

    $queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue
    $queues | ft -property Name,MessagesInQueue -HideTableHeaders
    

    就此而言,您应该只使用Format-Table 或任何Format- 命令来显示文本,而不是存储在变量中。如果您只想要前 4 个条目,您可以在 FT 之前通过管道传递到 Select 命令,例如:

    $queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue
    $queues | Select -First 4 | ft -property Name,MessagesInQueue -HideTableHeaders
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2018-12-28
      • 2022-12-08
      • 2017-08-23
      • 2022-12-22
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多