Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell脚本A调用脚本B,然后在脚本B仍在运行时关闭脚本A_Powershell_Powershell 3.0_Powershell 5.0 - Fatal编程技术网

PowerShell脚本A调用脚本B,然后在脚本B仍在运行时关闭脚本A

PowerShell脚本A调用脚本B,然后在脚本B仍在运行时关闭脚本A,powershell,powershell-3.0,powershell-5.0,Powershell,Powershell 3.0,Powershell 5.0,我目前有一个简单的脚本(a),它使用调用表达式来调用脚本B #Call Script $command = '\\BoxA\PowerShellScripts$\PS_CopyUIAutomationOutput.ps1' Try {Invoke-Expression $command } Catch { Write-host "Error: "$_ } 但当它这样做时,它会使脚本保持运行。我要做的是,脚本A调用脚本B,然后脚本A关闭,而脚本B仍在运行,记录到脚本B的共享路径,因此我不需要捕获

我目前有一个简单的脚本(a),它使用调用表达式来调用脚本B

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

但当它这样做时,它会使脚本保持运行。我要做的是,脚本A调用脚本B,然后脚本A关闭,而脚本B仍在运行,记录到脚本B的共享路径,因此我不需要捕获任何错误或登录脚本A。

只需使用
退出
关键字:

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

编辑:

如果希望在运行命令后直接退出,只需将其放在调用表达式之后,如下所示:

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

使用
停止进程
和自动变量
$PID
释放脚本A的会话并关闭PS窗口

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

只需使用
启动流程

Start-Process -FilePath PowerShell.exe -Argumentlist $Command
exit
这将在另一个进程中启动$command,并继续到下一行

问候,


Kvprasoon

遗憾的是,它在关闭之前仍然运行脚本B。