5

I've the following annoying problem. My custom VSTS build task won't fail. It always passes, while the $LASTEXITCODE is other then zero.

The code does as expected. It generates an error in the log. Despite that, the step succeeds and the build / release continues.

Screenshot: screenshot I've included a write-host with the exit code as well which shows exitcode 1 as well.

Code:

Try {
    ....
    #Loop through the server list
    Foreach ($Server in $machines) 
    {
        # Use SSL or not    
        If($UseSSL -eq $true) 
        {
            Write-Host "Connecting to $Server using a SSL connection (TCP/5986), Skip CA Check: $CheckCA ..."
            $s = New-PSSession -ComputerName $Server -Credential $Cred -UseSSL -SessionOption $SessionOptions
        }
        Else
        {
            Write-Host "Connecting to $Server with an unsecure connection (TCP/5985) ..."
            $s = New-PSSession -ComputerName $Server -Credential $Cred
        }

        # Run
        $ExitCode = Invoke-Command -Session $s -ScriptBlock $script -ArgumentList $ApplicationPoolName,$Action,$Killswitch

        # Cleanup the session
        Write-Host "Closing connection to $Server."
        Remove-PSSession -Session $s
    }
} Catch {
    Write-Host "##vso[task.logissue type=Error;]$Error"
    $ExitCode = 1
} Finally {
    #Leave TFS/VSTS trace
    if (Get-Command -Name Trace-VstsEnteringInvocation -ErrorAction SilentlyContinue) {
        Trace-VstsLeavingInvocation $MyInvocation
    }
    write-host "ExitCode: $ExitCode"
    Exit $ExitCode
}

What am i missing here?

  • 1
    According to Microsoft documentation, any non zero code should be flagged as failure. https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/powershell?view=vsts By default, the last exit code returned from your script will be checked and, if non-zero, treated as a step failure. >> By default, the last exit code returned from your script will be checked and, if non-zero, treated as a step failure. So the exitcode = 1 in the catch should make the step fail. That was my thought process. – Peter the Automator Sep 11 '18 at 12:19
  • You right then it should be failed. I was misreading. – Martin Backasch Sep 11 '18 at 12:35
  • But once a while I was facing the opposite problem with robocopy. It returns an exit code lower then 8, when their wasn't any failure during the copy operation. I solved the issue by checking if the `$LASTEXITCODE -lt 8` and setting the`$global:LASTEXITCODE = 0;` and then exit my powershell with `exit 0`. If there was an error I just throw an exception with `throw (ExitCode: $($LASTEXITCODE)]")`. You could try setting `$global:LASTEXITCODE = 1;` or throwing an exception. – Martin Backasch Sep 11 '18 at 12:52
  • @MartinBackasch I just tried the global and the throw of an exception, but the false positive remains – Peter the Automator Sep 11 '18 at 13:17
  • @Pba Just try using `Write-Error` in combination with an `exit 1`, reference this similar thread: https://stackoverflow.com/questions/34501891/how-to-fail-the-build-from-a-powershell-task-in-tfs-2015 – Andy Li-MSFT Sep 12 '18 at 06:57

2 Answers2

3

I solved it by removing the finally part.

Not working:

try {
    .... do stuff ....
} catch {
   write-error "some error"
   exit 1
} Finally {
  .. some final steps ...
}

Working:

try {
    .... do stuff ....
} catch {
   write-error "some error"
   exit 1
} 
0

For people arriving here, having the same problem with BAT/CMD files, my solution was to add this line:

exit /b %ERRORLEVEL%

to force transmitting the errorlevel to VSTS/TFS Command-Line Task

Wizou
  • 1,336
  • 13
  • 24