【发布时间】:2019-03-15 10:06:15
【问题描述】:
我想在 power shell 变量中存储一个绝对文件路径,并使用它来执行带有参数的程序。我想在构建服务器上执行此操作,这样我就可以在构建代理使用的构建服务器上拥有一个实用程序。考虑一下:
$pathToExe = "C:\path\to\program.exe"
#build server does stuff in a working dir
$pathToExe arg1 arg2
运行时,$pathToExe 不会扩展为它的值。相反,我在日志中看到:“表达式或语句中出现意外的标记 'arg1'。”
在 power shell 中做这种事情的正确方法是什么?我尝试了其他带引号的语法,但如果不对文件路径进行硬编码,我就无法让它工作,这是我试图避免的。
【问题讨论】:
-
我认为你需要使用
Start-Process -
怎么样?像这样?启动过程 $pathToExe arg1 arg2
-
这是我目前使用的一个示例。我对参数进行了硬编码,但可以使用变量。
Start-Process -FilePath ($Firefox + "Firefox_62.exe") -ArgumentList ("-ms /INI=$Firefox" + "Firefox_Settings.ini") -Verbose -Wait -NoNewWindow
标签: powershell variables filepath