【问题标题】:Execute Powershell Script from powershell indicate it executed successfullyExecute Powershell Script from powershell 表示执行成功
【发布时间】:2014-11-25 14:58:43
【问题描述】:

我正在尝试从另一个 powershell 脚本执行一个 powershell 脚本。我想要某种方式来表明脚本执行成功,或者它失败了。如果它失败了,我想传递异常。

这是我的基本脚本...

$Scripts = gci -Path $BuildForgeDir -filter "*.ps1"

如你所见,我找到了所有的 powershell 脚本...

ForEach($File in $Scripts){
    Write-host("File: $File")
    & "$BuildForgeDir\$File" -projName $projName  -baseBfDir $baseDirA -debugOrRelease $debugOrRelease
}

然后我执行它们...

我将如何冒泡异常、失败等...

【问题讨论】:

    标签: powershell


    【解决方案1】:

    异常自动冒泡...如果您仍然有疑虑,一定还有其他问题,请在问题中澄清..

    至于脚本的成功/失败。脚本也可以被认为是文件中的一个函数。即它们可以有一个param 块来获取参数,以及return 数据。在你的情况下。您可以让它返回一个指示成功或失败的标志。

    【讨论】:

      【解决方案2】:

      您可以使用 powershell.exe 调用另一个脚本,然后检查 $LastExitCode 是否为 0(0 表示成功)。这就是 Power Shell 检查外部应用程序状态的方式。

      例子:

      powershell.exe -noprofile -file E:\test.ps1
      If($LastExitCode -ne 0) {Throw "Error!"}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        • 2012-07-31
        • 1970-01-01
        相关资源
        最近更新 更多