2013-04-02 95 views
3

我有一個PROGRAMM我通常這樣開始在PowerShell中:啓動此參數的背景.exe文件在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腳本中運行。

回答

5

該作業將是PowerShell.exe的另一個實例,它不會以相同的路徑啓動,因此.將不起作用。它需要知道storage.exe在哪裏。

此外,您必須使用scriptblock中參數列表中的參數。您可以使用內置的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" 
+0

命名參數版本的外觀如何?我已經使用$ args來調用腳本,所以我不能使用args數組。 – mles

+0

您仍然可以使用參數。它在腳本塊中有一個新的作用域(它將用於作業的另一個powershell.exe實例中),但是我已更新以顯示命名參數。 –

+0

啊好的。現在我遇到了另一個問題。你也可能知道這一個? http://stackoverflow.com/questions/15769126/expand-variable-in-block – mles