【问题标题】:Use powershell variable to execute a program at the filepath in the variable使用powershell变量在变量中的文件路径执行程序
【发布时间】: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


【解决方案1】:

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

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

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

请参阅Get-Help about_Operators 或此MSDN link

【讨论】:

  • 我不知道这种方法。我喜欢。 ss64.com/ps/call.html 了解更多信息。谢谢@Pawel Dyl
  • call 运算符也称为invocation 运算符。
  • 谢谢!当我有 $executable = "C:\path\program.exe" 时,这非常有效。
猜你喜欢
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多