簡體   English   中英

將字符串傳遞給 PowerShell 腳本時出現意外的令牌“}”

[英]Unexpected token '}' when passing string to PowerShell script

我有一個接受兩個字符串的腳本,每個字符串由'|-|'分隔 . 第二個字符串可以包含除'之外的任何字符。

要調用我做的腳本:

PowerShell .\script.ps1 -arg1 'Hello|-|World' -arg2 'random|-|dwua0928]43d}'

它會在字符}上引發錯誤。 我該如何解決這個問題?

編輯:分隔符中的-也存在問題並引發相同的錯誤:

At line:1 char:54
+ .\Script.ps1 -arg2 <redacted>|-|<redacted> ...    
+                                ~
Missing expression after unary operator '-'.
At line:1 char:53
+ .\Script.ps1 -arg1 <redacted>|-|<redacted> ...    
+                               ~
Expressions are only allowed as the first element of a pipeline.

它在|-|的每個實例中都這樣做 . 如果我將分隔符更改為/-/ ,它會使用方括號:

At line:1 char:168
+ ... <redacted>/-/<redacted>!OW}:Lj<redacted> ...
+                               ~
Unexpected token '}' in expression or statement.
At line:1 char:181
+ ... /-/<redacted>cO0}e]k<redacted> ...
+                     ~
Unexpected token '}' in expression or statement.

腳本的開頭是這樣的:

Param
(
    [switch] $enable,
    [string] $arg1,
    [string] $arg2
)
  • There's generally no need to call the PowerShell CLI ( powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+) from inside PowerShell.

    • 假設您正在運行Windows PowerShell (或者概括地說,相同版本的 PowerShell),您可以直接調用您的腳本:

       .\script.ps1 -arg1 'Hello|-|World' -arg2 'random|-|dwua0928]43d}'
  • 如果您仍然想調用 CLI - 這很昂貴,因為它涉及創建進程,並且還會導致類型保真度的損失 - 使用腳本塊( {... } ),但請注意,這僅適用於inside PowerShell (though it works across both PowerShell editions, ie, you can call pwsh.exe from Windows PowerShell, and powershell.exe from PowerShell (Core) 7+)

     powershell {.\script.ps1 -arg1 'Hello|-|World' -arg2 'random|-|dwua0928]43d}' }
  • 如果要從 PowerShell外部調用,則需要使用 -File CLI 參數而不是(在-File PowerShell 中隱含) -Command參數:

     powershell -File.\script.ps1 -arg1 "Hello|-|World" -arg2 "random|-|dwua0928]43d}"
    • 雖然這也適用於 PowerShell 內部,但與{... }方法相比的缺點是 output 只是text ,而{... }方法 - 在限制范圍內 - 能夠保留原始數據類型 - 請參閱此答案以獲取更多信息。

有關 PowerShell CLI 的全面概述,請參閱這篇文章


至於你嘗試了什么:

  • 您調用powershell.exe時既沒有-File ( -f ) 也沒有-Command ( -c ),默認為-Command (請注意,相比之下, pwsh ,PowerShell (Core) 7+ CLI,默認為-File )。

  • 當使用-Command時,PowerShell 的命令行處理首先去除所有后續 arguments 的任何語法(即未轉義) "字符,即來自"..." - 封閉的 arguments,將生成的標記與空格連接,然后解釋生成的字符串為 PowerShell 代碼

  • 在您的情況下,您在 arguments 周圍使用'...'引用的事實無關緊要:

    • PowerShell 在調用外部程序時總是將原始引用轉換為"..."引用(因為它們只能被"..."引用,而不是'...'引用)
    • 但是,它僅在需要時使用"..."引用,這僅基於手頭的參數是否包含空格

您的 arguments,逐字Hello|-|Worldrandom|-|dwua0928]43d}包含空格,因此它們按原樣放置在powershell.exe的命令行上(盡管請注意,即使它們確實包含空格和因此,PowerShell 的命令行處理已被包含在"..."中,因此會剝離"字符。)

最終結果是powershell.exe子進程嘗試執行以下 PowerShell 代碼,這可以預見地與您的問題中顯示的錯誤中斷:

# !! Note the absence of quoting around the -arg1 and -arg2 values.
.\script.ps1 -arg1 Hello|-|World -arg2 random|-|dwua0928]43d}

您可以輕松地從 PowerShell session 中引發如下錯誤:

# -> Error "Missing expression after unary operator '-'."
Write-Output Hello|-|World

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM