xs2rashid
xs2rashid

Reputation: 1053

Powershell script error when output Array inside if statement

I am running following script in powershell script and getting error.

$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue

$list = $queues | ft -property Name,MessagesInQueue

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

if ($i -gt 2) {

$list[$i] 

}

}

Error: out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-*" command which is conflicting with the default formatting. + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

Upvotes: 1

Views: 1759

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36297

It looks like you are simply trying to skip the headers for your data when displayed with Format-Table (or FT for short as you used). To do that just use the -HideTableHeaders switch on your FT command, and don't capture it in a variable.

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

For that matter, you should only use Format-Table or any of the Format- commands to display text, not to store in a variable. If you only want the first 4 entries you would pipe to a Select command before the FT like:

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

Upvotes: 2

Related Questions