执行参数字符串中带括号的powershell

本文关键字:powershell 参数 字符串 执行 | 更新日期: 2023-09-27 17:52:54

我们有一个powershell脚本,我们是从一个c# . net 4.5程序中剥离出来的,它运行在带有powershell 4的系统上。它的主要目的之一是查找并停止相关的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行char:52+ c: ' psrunner '机器人。ps1 -source Neutrino update (support.internal)定……+ ~~~~~~~~~~~~~~~~+ CategoryInfo: ObjectNotFound: (support.internal:String) [], Co . mandnotfoundexception+ fulllyqualifiederrid: CommandNotFoundException

执行参数字符串中带括号的powershell

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

如果您不想使用单引号(例如,在launch中),请使用``括号转义。

/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"