Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-46876

PowerShell: incorrect handling of $LASTEXITCODE can cause broken script to not abort pipeline



    • Bug
    • Status: Closed (View Workflow)
    • Minor
    • Resolution: Fixed
    • durable-task-plugin
    • None
    • Windows 10 x64
      JRE 1.8u144
      Jenkins 2.78
      Durable Task Plugin 1.14
      Jenkins installed as Standard Windows Service without a separate Webserver or Proxy


      Recently one of our PowerShell scripts failed but the pipeline didn't abort because Jenkins considered the PowerShell script to be succesful.

      You can reproduce the problem with the following example pipeline script:

      node {
        powershell '''
          $ErrorActionPreference = 'Stop'
          & cmd /c 'exit 0'
          Copy-Item 'doesnt' 'exist'

      This script should fail because of ErrorActionPreference = 'Stop' and because Copy-Item will return an error because the file doesn't exist (resulting in $? = False).

      I assume that this happens because the PowerShell plugin first checks for $LASTEXITCODE, which was set to 0 by the "cmd" command. The problem is that $? isn't checked as well.


        Issue Links


            mus65 m t created issue -
            gabloe Gabriel Loewen made changes -
            Field Original Value New Value
            Assignee Gabriel Loewen [ gabloe ]
            gabloe Gabriel Loewen made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            jglick Jesse Glick made changes -
            Status In Progress [ 3 ] In Review [ 10005 ]
            jglick Jesse Glick made changes -
            Link This issue is duplicated by JENKINS-48175 [ JENKINS-48175 ]
            jglick Jesse Glick made changes -
            Link This issue is duplicated by JENKINS-48325 [ JENKINS-48325 ]
            svanoort Sam Van Oort made changes -
            Resolution Fixed [ 1 ]
            Status In Review [ 10005 ] Closed [ 6 ]


              gabloe Gabriel Loewen
              mus65 m t
              1 Vote for this issue
              4 Start watching this issue