我正在尝试运行以下代码来检索计算机上的本地用户列表。
gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
Format-Table Name,Description
在 PS1 文件中运行时出现此错误:
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
我知道这个问题是由于管道的解析方式引起的,但我不知道如何解决它。
最佳答案
Format-*
cmdlet 不执行最终输出,而是将其输入转换为一系列格式化对象。这些格式化对象由 Out-
cmdlet 之一(可能是 Out-Default
)转换为实际输出。
如果脚本具有多个不同的格式化对象集,则脚本中所有表达式的合并对象的最终输出Out-Default
无法解决不一致问题。
修复:将 Out-Sting
添加到每个输出生成管道的末尾,以一次执行一个表达式的格式化:
gwmi win32_useraccount -Computername $env:computername -Filter "Domain='$env:computername'" |
Format-Table Name,Description | Out-String
关于Powershell格式表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7517581/