【问题标题】:Powershell command line parameter with single quote will break command line带有单引号的 Powershell 命令行参数将中断命令行
【发布时间】:2018-10-04 14:06:15
【问题描述】:

我正在从批处理文件中调用 powershell 脚本

powershell createshortcut.ps1 "%~n0"

但是,如果参数有单引号(扩展示例)

powershell createshortcut.ps1 "Divertirsi con l'ortografia"

解析器会抛出错误

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

显然参数的内容是未知的。

Powershell 版本:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  2189

【问题讨论】:

    标签: powershell


    【解决方案1】:

    告诉 PowerShell 它将处理 -File 而不是 -Command

    powershell -NoProfile -File createshortcut.ps1 "Divertirsi con l'ortografia"
    

    【讨论】:

      【解决方案2】:

      使用单个反引号 (`) 将其转义:

      powershell createshortcut.ps1 "Divertirsi con l`'ortografia"
      

      或者用简单的双引号替换双引号并使用''

      powershell createshortcut.ps1 'Divertirsi con l''ortografia'
      

      【讨论】:

      • 事先不知道参数的内容是什么
      • 您可以在批处理脚本中执行替换,然后再将其传递到 PowerShell。
      猜你喜欢
      • 1970-01-01
      • 2012-01-21
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 2011-03-24
      • 2013-03-24
      相关资源
      最近更新 更多