[英]How do I pass a string as a non-string to a command in powershell?
[英]How do I pass in a string with spaces into PowerShell?
鑒於:
# test1.ps1
param(
$x = "",
$y = ""
)
&echo $x $y
像這樣使用:
powershell test.ps1
輸出:
> <blank line>
但是后來出錯了:
test.ps1 -x "Hello, World!" -y "my friend"
輸出:
Hello,
my
我期待看到:
Hello, World! my friend
嗯,這是一個cmd.exe
問題,但有一些方法可以解決它。
使用單引號
powershell test.ps1 -x 'hello world' -y 'my friend'
使用-file
參數
powershell -file test.ps1 -x "hello world" -y "my friend"
使用以下內容創建一個.bat
包裝器
@rem test.bat @powershell -file test.ps1 %1 %2 %3 %4
然后調用它:
test.bat -x "hello world" -y "my friend"
可以使用反引號 ` 來轉義空格:
PS & C:\Program` Files\\....
在我的情況下,一個可能的解決方案是嵌套單引號和雙引號。
test.ps1 -x '"Hello, World!"' -y '"my friend"'
我有一個類似的問題,但在我的情況下,我試圖運行一個 cmdlet,並且調用是在 Cake 腳本中進行的。 在這種情況下,單引號和-file
參數不起作用:
powershell Get-AuthenticodeSignature 'filename with spaces.dll'
結果錯誤: Get-AuthenticodeSignature : A positional parameter cannot be found that accepts argument 'with'.
如果可能,我想避免使用批處理文件。
解決方案
有效的是使用帶有 /S 的 cmd 包裝器來解開外部引號:
cmd /S /C "powershell Get-AuthenticodeSignature 'filename with spaces.dll'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.