power shell 錯誤:無法綁定參數

power shell 錯誤:無法綁定參數

當我嘗試執行以下 PowerShell 命令時,出現錯誤。

命令 :

*PS 憑證:\目前使用者\authroot> gci |其中主題類似“UTN「*

錯誤如下:

Where-Object:無法綁定參數「FilterScript」。無法將「System.String」類型的「subject」值轉換為「System.Management.Automation.ScriptBlock」類型。在行:1 字元:12 + gci |其中<<<<類似主題“UTN" + CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException + ExcellentQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand

我正在使用 Windows PowerShell ISE。
高度讚賞可行的解決方案

答案1

看起來您正在使用 PowerShell Vs.2,該版本不支援新的where syntax.

在 PowerShell 版本 1 和 2 中使用:

gci | where {$_.subject -like "UTN"}

您需要在表達式兩邊加上大括號,並引用$_.任何帶有前綴的屬性。

答案2

Peter Hahndorf 已經回答了這個問題,但我想詳細說明您收到的錯誤訊息:

無法將「System.String」類型的「subject」值轉換為 System.Management.Automation.ScriptBlock 類型

這就是說它無法將 a 轉換string為 a scriptBlock,這意味著後面where需要跟一個腳本區塊,如下所示:{code here}

請務必閱讀錯誤訊息並嘗試解釋它們的含義。

答案3

使用“-match”尋找可能位於主題中任何位置的 UTN

gci | ?{$_.subject -match "UTN"}

如果您使用「-like」但沒有顯示任何內容,請將您要尋找的內容放在引號內的星號之間。

gci | ?{$_.subject -like "*UTN*"}

相關內容