【发布时间】:2016-07-04 08:18:01
【问题描述】:
我想从 PowerShell 脚本调用一个可执行文件,该脚本需要在参数列表中的特定位置加上引号。尽管我发现了类似的问题,但我根本没有找到解决方案。
这是命令在命令行上的样子:
reptool.exe --profile="C:\My profile"
参数值(“C:\Profiles...”)应该是使用变量动态生成的:
$repToolProfile = "C:\My profile"
这是我已经尝试过的:
&"reptool.exe" --profile=$repToolProfile
失败,因为参数以"--profile=C:\My profile"(整个参数的引号)给出。
&"reptool.exe" --profile="$repToolProfile"
失败,因为参数以"--profile=C:\My profile" 给出(整个参数的引号,同上)。
&"reptool.exe" "--profile=`"$repToolProfile`"
失败,因为参数以"--profile="C:\My profile"" 形式给出(在整个参数和值周围加上引号)。
我不能使用单引号或“逐字运算符”(--%),因为我必须使用 PowerShell 变量,也不能使用 Start-Process,因为它被异步调用(即使我使用 -Wait 参数. 另外我想检查退出代码。我不想将我的参数转换为 Base64。
【问题讨论】:
-
试试这个:
&"reptool.exe" @{'--profile=' = ('"{0}"' -f $repToolProfile)} -
不起作用。参数扩展为
System.Collections.Hashtable。 -
&"reptool.exe" '--%' "--profile=`"$repToolProfile`""
标签: powershell