【问题标题】:Call an executable with quotes at specific positions在特定位置调用带有引号的可执行文件
【发布时间】: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


【解决方案1】:

这对我有用:

$command = '& "reptool.exe" --% ' + "--profile=`"$repToolProfile`"
Invoke-Expression $command

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2015-04-07
    • 2019-11-17
    • 2016-06-13
    相关资源
    最近更新 更多