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

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

XMLWordPrintable

    • 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.

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

              Created:
              Updated:
              Resolved: