开发者

Powershell format-table error

开发者 https://www.devze.com 2023-04-06 23:07 出处:网络
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\'\" |

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 figur开发者_运维问答e out how to get around it.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号