2011-09-22 43 views
17

我試圖運行下面的代碼來檢索一臺機器上的本地用戶列表。Powershell格式表錯誤

 
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 

我理解這個問題的產生是因爲方式的管道被分析,但是我不能想出如何繞過它:

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

我一個PS1文件中運行時出現此錯誤。

回答

20

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 
+0

原始命令對我來說不會出錯。是什麼賦予了? – manojlds

+0

我不會用這個命令重現我的七個,但是它會追加到其他WMI類中。 – JPBlanc

+0

@manojlds在單個執行中一起運行多個輸出生成語句時,問題就出現了。通過在命令行上使用';'語句分隔符將兩個命令組合在一起,可以在命令行上獲得相同的結果。在某些情況下它有效,在另一些情況下則不行。但強制輸出是直接顯示的字符串一直對我有用。 – Richard

1

你也可以嘗試:

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

其實你轉換到中間PSCustomObject,你仍然有一個對象。