2013-04-02 26 views
2

可變我有一個shell腳本應該在後臺啓動一個.exe文件:解釋在PowerShell中{}腳本塊

$strPath = get-location 
$block = {& $strPath"\storage\bin\storage.exe" $args} 
start-job -scriptblock $block -argumentlist "-f", $strPath"\storage\conf\storage.conf" 

在前面的Question我發現我需要絕對路徑。然而,$ strPath的變量沒有解釋,如果你看一下下面的命令:

PS Q:\mles\etl-i_test> .\iprog.ps1 --start1 
Start Storage 

Id    Name   State  HasMoreData  Location    Command 
--    ----   -----  -----------  --------    ------- 
37    Job37   Running True   localhost   & $strPath"\storage\bi... 

我怎樣才能解決這個問題?

編輯:我知道我需要通過路徑作爲參數,以及如何?例如:

$block = {& $args[0]"\storage\bin\storage.exe" $args[1] $args[2]} 
start-job -scriptblock $block -argumentlist $strPath, "-f", $strPath"\storage\conf\storage.conf" 

+0

在$ block中變量$ strPath是空的,因爲scriptblock是一個全新的腳本。你的腳本塊需要接受參數。噢,[不要使用匈牙利語](http://windowsitpro.com/blog/what-do-not-do-powershell-part-5)。不是一個大問題,但實踐使得完美。 –

回答

4

腳本塊的內容將在另一個無法訪問變量的PowerShell.exe實例中執行(作爲作業)。這就是爲什麼您需要將它們發送到Start-Job參數列表中。發送作業所需的所有數據作爲參數。例如storage.exe的完整路徑,例如

$path = (Get-Location).Path 
$block = {& $args[0] $args[1] $args[2]} 

start-job -scriptblock $block -argumentlist ` 
    "$path\storage\bin\storage.exe" ` 
    "-f", ` 
    "$path\storage\conf\storage.conf" 
+0

我使用這個http://blogs.technet.com/b/heyscriptingguy/archive/2013/05/22/variable-substitution-in-a-powershell-script-block.aspx,但我更喜歡你的解決方案。謝謝。 –

+0

你在答案中缺少一列嗎? –