Powershell格式表错误

标签 powershell

我正在尝试运行以下代码来检索计算机上的本地用户列表。

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/

相关文章:

powershell - 创建并抛出新异常

powershell - 从 PowerShell ISE 的命令环境加载脚本

c# - 为什么 C# Powershell 方法在 Windows 应用程序中有效,但在 Web 应用程序中无效

PowerShell DSC 拉和推模式一起

powershell - 验证默认值

powershell - Get-AzureStorageBlob : Could not get the storage context. 请传入存储上下文或设置当前存储上下文

powershell - 通过动态重定向到文件从另一个Powershell脚本内部运行Powershell脚本

powershell - 根据版本号和一些条件比较文件并格式化输出

通过过滤多个条件重命名 Powershell 文件

postgresql - PSQL 不提示在 PowerShell 中输入密码