Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell format-table error

Tags:

powershell

I am attempting to run the following code to retrieve a list of local users on a machine.

gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
  Format-Table Name,Description

I get this error when running inside a PS1 file:

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

I understand this issue arises because of the way the pipelines are parsed but I can't figure out how to get around it.

like image 585
user708516 Avatar asked Sep 22 '11 15:09

user708516


People also ask

How do you display data in a table Format in PowerShell?

The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.

How do you clear error variables in PowerShell?

What to do if you want to clean out all the entries in $error? $error is a variable, so you can try with the Clear-Variable cmdlet: PS> Clear-Variable error -Force Clear-Variable : Cannot overwrite variable Error because it is read-only or constant.

How do I stop a PowerShell script error?

You can use the ErrorAction parameter on every cmdlet and advanced function in PowerShell. It is a great way to choose which commands should stop script execution and which ones should not.

What is ErrorAction in PowerShell?

The PowerShell ErrorAction parameter allows handling the actions if any error occurs. By default, PowerShell operates on the continue option for error handling. However, the ErrorAction operator can be used to change the default option.


2 Answers

The Format-* cmdlets do not do final output, but transform their input into a sequence of formatting objects. These formatting objects are converted to the actual output by one of the Out- cmdlets, probably Out-Default.

If a script has multiple, different, sets of formatting objects that final output of the merged objects from all the expressions in the script Out-Default cannot resolve the inconsistencies.

Fix: add a Out-Sting to the end of each output generating pipeline to perform the formatting one expression at a time:

gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
  Format-Table Name,Description | Out-String
like image 106
Richard Avatar answered Oct 04 '22 02:10

Richard


you can also try :

gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'"  | Select-Object Name,Description  | Format-Table Name,Description

In fact you convert to an intermediate PSCustomObject and you still have an object.

like image 27
JPBlanc Avatar answered Oct 04 '22 01:10

JPBlanc