7

我有一个程序,我通常在 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 脚本中运行。

4

1 回答 1

9

该作业将是 PowerShell.exe 的另一个实例,它不会以相同的路径启动,因此.无法工作。它需要知道在哪里storage.exe

您还必须在脚本块中使用参数列表中的参数。您可以使用内置的 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"
于 2013-04-02T15:16:48.800 回答