在使用powershell脚本的jenkins管道中使用powershell脚本时,无法捕获异常消息。

问题描述 投票:0回答:2
我有一个令人讨厌的脚本,该脚本运行了詹金斯管道,并包含一个powershell脚本。但是,当PowerShell脚本抛出异常时,我无法在捕获块中打印异常消息。这是我的代码:

def call() { node { timestamps { ansiColor('xterm') { try { powershell """ throw new Exception("ERROR: This is a test Exception.") """ } catch (error) { println("Caught error: ${error.getMessage()}") } } } } }
我尝试使用error.message,error.printStackTrace()和error.cause.getMessage(),但它们都没有打印错误消息。相反,我收到消息“脚本返回的退出代码1”。
有人可以帮助我正确捕获错误消息吗?

我的理解是,您不能以相同的方式将异常从Powershell转到Groovy。

最终的行动课程将在您的PowerShell脚本中捕获异常,写入输出,没有错误,然后解析输出以解决此错误。 像这样的东西:

powershell jenkins groovy
2个回答
2
投票

我没有解释,为什么詹金斯·乔布斯(Jenkins Jobs)对文档中所述的powershell错误没有反应:

https://www.jenkins.io/blog/2017/07/07/26/powershell-pipeline/powershell-pipeline/

the wher the try
-ErrorAction Stop

0
投票
  • 或设置

    $ErrorActionPreference = 'Stop'
  • 示例
    try {
        powershell """
            throw new Exception("ERROR: This is a test Exception.") -ErrorAction Stop
        """
    }
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.