使用 powershell 脚本中的参数在后台启动 .exe

2023-12-19

我有一个程序,通常在 powershell 中这样启动:

.\storage\bin\storage.exe -f storage\conf\storage.conf

在后台调用它的正确语法是什么?我尝试了很多组合,例如:

start-job -scriptblock{".\storage\bin\storage.exe -f storage\conf\storage.conf"}
start-job -scriptblock{.\storage\bin\storage.exe} -argumentlist "-f", "storage\conf\storage.conf"

但没有成功。它还应该在 powershell 脚本中运行。


该作业将是 PowerShell.exe 的另一个实例,并且不会在同一路径中启动,因此.行不通的。它需要知道在哪里storage.exe is.

此外,您还必须使用脚本块中参数列表中的参数。您可以使用内置的 args 数组或执行命名参数。 args 方式需要最少的代码。

$block = {& "C:\full\path\to\storage\bin\storage.exe" $args}
start-job -scriptblock $block -argumentlist "-f", "C:\full\path\to\storage\conf\storage.conf"

命名参数有助于了解参数应该是什么。使用它们的效果如下:

$block = {
    param ([string[]] $ProgramArgs)
    & "C:\full\path\to\storage\bin\storage.exe" $ProgramArgs
}
start-job -scriptblock $block -argumentlist "-f", "C:\full\path\to\storage\conf\storage.conf"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 powershell 脚本中的参数在后台启动 .exe 的相关文章

随机推荐