2017-01-31 79 views
0

我現在有一個簡單的腳本(A),使用調用,表達式調用腳本B.PowerShell腳本A調用腳本B,然後關閉腳本A,而腳本B仍在運行

#Call Script 
$command = '\\BoxA\PowerShellScripts$\PS_CopyUIAutomationOutput.ps1' 
Try 
{Invoke-Expression $command } 
Catch 
{ 
Write-host "Error: "$_ 
} 

但是,當它這樣做它保持腳本A運行。我想要做的是腳本A調用腳本B,然後腳本A關閉,而腳本B仍在運行日誌記錄已完成腳本B的共享路徑,所以我不需要捕獲任何錯誤或登錄腳本A 。

回答

1

只需使用exit關鍵字:

Try 
{Invoke-Expression $command } 
Catch 
{ 
Write-host "Error: "$_ 
} 
exit 

編輯:

如果你想讓它直接運行該命令只把它像這樣的invoke表達後後退出:

Try 
{Invoke-Expression $command 
exit} 
Catch 
{Write-host "Error: "$_ 
} 
+0

可悲的是沒有,它仍然在關閉之前運行腳本B. –

+0

@ScottE看到我的編輯 –

0

使用Stop-Process和自動變量$PID來釋放腳本A的會話並關閉PS窗口。

$command = '\\BoxA\PowerShellScripts$\PS_CopyUIAutomationOutput.ps1' 
Try 
{ 
Invoke-Expression $command 
} 
Catch 
{ 
Write-host "Error: "$_ 
} 
Stop-Process -Id $PID 
0

只需使用Start-Process

Start-Process -FilePath PowerShell.exe -Argumentlist $Command 
exit 

,這將在不同的進程啓動$命令,繼續下一行

問候,

Kvprasoon