【问题标题】:executing powershell with brackets in argument string在参数字符串中使用括号执行 powershell
【发布时间】:2015-08-17 17:24:18
【问题描述】:

我们有一个 powershell 脚本,我们在使用 powershell 4 的系统上从 c# .Net 4.5 程序中脱壳。它的主要目的之一是查找并停止关联的 Windows 服务,以便我们对其进行维护。

问题是在某些网站上,服务的名称在名称中包含方括号,即

Acme 管理服务 (support.internal)

这会导致 powershell 生成异常,因为我认为它正在尝试将 (support.internal) 解析为对象的属性。有没有办法避开括号,因为我们不太可能在现场重命名所有潜在的 Windows 服务名称。

复制此错误的示例如下

这是我们传递给 cmd.exe 的参数字符串。

/c powershell -executionpolicy unrestricted c:\psrunner\robotest.ps1 -source "Acme Management Service (support.internal)" -destination "" -logName "c:\psrunner\logs\\03062015_14_04_51.txt"  > "c:\psrunner\logs\\powershell_log03062015_14_04_51.txt"

当我们调用脚本时,即

support.internal :术语“support.internal”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 检查名称的拼写,或者如果包含路径,请验证 路径正确,然后重试。在行:1 字符:52 + c:\psrunner\robotest.ps1 -source Neutrino 更新程序(support.internal) -目的地... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (support.internal:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

  • 尝试使用反引号“`”或反斜杠进行转义,或者真的在任何地方进行服务重命名。

标签: c# powershell powershell-4.0


【解决方案1】:

单引号而不是双引号也应该让 PS 将输入视为纯字符串。

【讨论】:

    【解决方案2】:

    如果您不想使用单引号,只需使用 `` 括号转义(例如,在 Visual Studio Code 的 launch.json 中,字符串参数禁止使用单引号)

    /c powershell -executionpolicy unrestricted c:\psrunner\robotest.ps1 -source "Acme Management Service `(support.internal`)" -destination "" -logName "c:\psrunner\logs\\03062015_14_04_51.txt"  > "c:\psrunner\logs\\powershell_log03062015_14_04_51.txt"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2021-09-16
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多