输出数组内部if语句时出现Powershell脚本错误

时间:2016-10-13 16:21:37

标签: powershell

我在powershell脚本中运行以下脚本并收到错误。

$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue

$ list = $ queues | ft -property名称,MessagesInQueue

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

if($ i -gt 2)     {

$list[$i] 

}

}

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

1 个答案:

答案 0 :(得分:0)

看起来您只是在使用Format-Table(或FT显示时)跳过数据标题。要做到这一点,只需使用FT命令上的-HideTableHeaders开关,不要在变量中捕获它。

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

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

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