powershell - 如何在批处理文件的 powershell 命令中包含与号?
问题描述
我正在尝试通过批处理文件下载 Google 表格。这有效:
powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv -OutFile output.tsv"
当我通过添加 &gid=1234 指定我想要的工作表/选项卡时,这会中断:
powershell -Command "Invoke-WebRequest https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234 -OutFile output.tsv"
错误是:
不允许使用与号 (&) 字符。& 运算符保留供将来使用;用双引号 ("&") 将 & 号括起来以将其作为字符串的一部分传递。
如何在不破坏 Command 参数的外部引号的情况下将 & 号括在引号中?
解决方案
嵌入在"..."
传递给的字符串中的 URL 也powershell -Command
必须被引用,因为未引用的 URL 对PowerShell &
也具有特殊意义(尽管在 Windows PowerShell 中它目前仅保留以供将来使用;在 PowerShell Core中,它可以用于后定位运行命令作为后台工作)。
正如Olaf所建议的那样,最简单的选择是使用嵌入式'...'
引用,因为chars. 不需要逃到里面。PowerShell 中的字符串是文字字符串,在这种情况下很好,因为 URL 不包含变量引用。'
"..."
'...'
powershell -Command "Invoke-WebRequest 'https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234' -OutFile output.tsv"
"..."
如果字符串插值需要嵌入\"
引用,请使用(sic) 转义嵌入的 ( "
) 字符。(请注意,在PowerShell 中,您需要使用`"
or""
代替):
powershell -Command "Invoke-WebRequest \"https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/export?exportFormat=tsv&gid=1234\" -OutFile output.tsv"
推荐阅读
- reactjs - 将 react-pdf 渲染文件保存到状态以将其发送到服务器
- android - “颜色?”类型的值 不能分配给“颜色”类型的变量
- c++ - 如何在 Wayland 上的 Ubuntu 中计算系统空闲时间?
- java - 如何将复杂的业务逻辑保留在编排器方法之外(使用 SRP 和干净的架构理念)?
- react-native - 如何隐藏底部动画标签栏导航器?
- php - 未找到二级组织单位
- algorithm - Min Coin Change 自上而下的方法
- list - (颤振)列表视图构建器不会显示复选框列表
- php - Laravel 显示带有数据透视表错误的书籍类别
- java - 写入文件时Java文件为空白