使用 powershell 变量在变量中的文件路径处执行程序

Use powershell variable to execute a program at the filepath in the variable

我想将绝对文件路径存储在一个强大的 shell 变量中,并用它来执行带参数的程序。我想在构建服务器上执行此操作,这样我就可以在构建代理使用的构建服务器上拥有一个实用程序。考虑一下:

$pathToExe = "C:\path\to\program.exe"

#build server does stuff in a working dir

$pathToExe arg1 arg2

运行时,$pathToExe 不会扩展到它的值。相反,我在日志中看到:"Unexpected token 'arg1' in expression or statement."

当权者做这种事情的正确做法是什么shell?我已经尝试过其他带引号的语法,但如果不对文件路径进行硬编码,我将无法使用它,这是我试图避免的。

您应该使用呼叫运营商 &。请参阅以下示例:

$executable = 'notepad.exe'
$argument = 'E:\Temp\Test.xml'

& $executable $argument
& $executable 'E:\Temp\Test.xml'

请参阅 Get-Help about_Operators 或此 MSDN link