如何在批处理文件的powershell命令中包含“&"号?
我正在尝试通过批处理文件下载Google表格.这有效:
I am trying to download a Google sheet via a batch file. This works:
powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv -OutFile output.tsv"
当我通过添加& gid = 1234指定所需的工作表/标签时,这会中断:
When I specify which sheet/tab I want by adding &gid=1234, this breaks:
powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234 -OutFile output.tsv"
错误是:
不允许使用&字符. &运算符保留 供将来使用;用与引号将双引号(&")括起来 将其作为字符串的一部分传递.
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
如何将&符括在引号中而不破坏Command参数的外部引号?
How do I wrap the ampersand in quotes without breaking the outer quotes for the Command parameter?
传递给powershell -Command
的"..."
字符串中嵌入的URL也必须加上引号,因为 > unquoted &
对PowerShell也具有特殊的意义(尽管在Windows PowerShell中,当前仅保留供将来使用;在PowerShell Core 中,可以在位置后使用).来将命令作为后台作业运行.)
The URL embedded inside the "..."
string passed to powershell -Command
must be quoted too, because an unquoted &
has special meaning to PowerShell too (though in Windows PowerShell it is currently only reserved for future use; in PowerShell Core it can be used post-positionally to run a command as a background job).
最简单的选项是使用嵌入式'...'
引号,如 Olaf 所建议,因为'
字符.不需要在"..."
内转义. PowerShell中的'...'
字符串是 literal 字符串,在这种情况下,考虑到URL不包含变量引用,就可以了.
The simplest option is to use embedded '...'
quoting, as suggested by Olaf, because '
chars. don't need escaping inside "..."
. '...'
strings in PowerShell are literal strings, which is fine in this case, given that the URL contains no variable references.
powershell -Command "Invoke-WebRequest 'https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234' -OutFile output.tsv"
如果需要对字符串内插使用嵌入的"..."
引号,请使用\"
(原文如此)转义嵌入的("
)字符. (请注意,在内部 PowerShell中,您需要使用`"
或""
代替):
If embedded "..."
quoting is needed for string interpolation, use \"
(sic) to escape the embedded ("
) chars. (note that inside PowerShell, you'd need to use `"
or ""
instead):
powershell -Command "Invoke-WebRequest \"https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234\" -OutFile output.tsv"