首页 > 解决方案 > 从当前脚本打开另一个 powershell 终端以执行当前脚本输出

问题描述

    $argList = "-file `"C:\Users\bdl\Desktop\jhansi\PowerShell_Scripts\dialog.ps1`""
Start powershell -argumentlist $argList -NoNewWindow

我正在尝试从当前脚本打开另一个 powershell 终端以执行当前脚本输出。另一个 powershell 终端正在打开,但它一直在闪烁。我写了上面两行代码,但它在闪烁。请告诉我上面两行中的错误在哪里。

标签: windowspowershell

解决方案


你可以尝试这样的事情:

$ArgList = "C:\Users\bdl\Desktop\jhansi\PowerShell_Scripts\dialog.ps1"
Start-Process -FilePath PowerShell -ArgumentList $ArgList -NoNewWindow -Wait

您还可以通过在其中添加退出代码来检查在 PowerShell 中调用的脚本是否成功返回(退出 0 表示成功,退出 1 表示失败):

$Exe = (Start-Process -FilePath PowerShell -ArgumentList $ArgList -NoNewWindow -Wait -PassThru).ExitCode

If ($Exe -ne 0)
{
    Write-Host "An error has occured while running the script."
}

由于 0 以外的退出代码意味着脚本没有正确完成。


推荐阅读