在参数字符串中用括号执行 powershell

executing powershell with brackets in argument string

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

问题是在某些网站上,服务名称中有括号,即

Acme Management Service (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 : The term 'support.internal' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:52 + c:\psrunner\robotest.ps1 -source Neutrino Updater (support.internal) -destinatio ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (support.internal:String) [], Co mmandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

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

如果你不想使用单引号就用``括号转义(例如,在launch.json in Visual Studio代码中,字符串参数禁止使用单引号)

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