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

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

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • 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.

          [JENKINS-46876] PowerShell: incorrect handling of $LASTEXITCODE can cause broken script to not abort pipeline

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

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

              Created:
              Updated:
              Resolved: