Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding errors to build summary from PowerShell script running in VSTS

In VSTS, I am using a PowerShell task to run one of my script. The script contains the following:

Write-Error 'error 1'
Write-Error 'error 2'

Since the Write-Error cmdlet writes on stderr, these messages get reported in the VSTS web interface:

errors

I would like to improve on this output. The improvements that I am looking for is:

  1. I would like to have one red X per error.
  2. I would like to remove all the noise, i.e. get rid of the lines starting with '+'

Instead of calling Write-Error, is there another function that I can call from my script to add an error to the build summary page?

I saw the function Write-VstsTaskError but unfortunately it can only be called from a VSTS task. It cannot be called from my script.

like image 820
mabead Avatar asked Feb 10 '16 18:02

mabead


People also ask

How do I write a PowerShell error?

To write a non-terminating error, enter an error message string, an ErrorRecord object, or an Exception object. Use the other parameters of Write-Error to populate the error record. Non-terminating errors write an error to the error stream, but they do not stop command processing.

How do I stop a PowerShell script error?

In that case, use Stop for the ErrorAction . When you run a command using the Stop value, PowerShell treats any errors returned as terminating and acts accordingly. You can control non-terminating and terminating errors globally using the $ErrorActionPreference variable.

How do you ignore an error in PowerShell and let it continue?

If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.


2 Answers

The recommended way to do this is using VSO logging commands as discussed in the GitHub issue below.

  • Is it possible to raise and display build warnings from build steps using TFS 2015?

  • Logging Commands

For example:

Write-Host "##vso[task.logissue type=warning;]Test warning"
Write-Host "##vso[task.logissue type=error;]Test error"
like image 128
Matt Brooks Avatar answered Sep 24 '22 03:09

Matt Brooks


After lots of trial and error, I was able to solve problem #1. The VSTS build displays a red X for each entry in stderr that is seperated by an entry on stdout. So, if I change my script to the following:

Write-Error 'Error 1'
Write-Host '***'
Write-Error 'Error 2'
Write-Host '***'

I now have one red X per error, but I still have the extra noise (lines starting with '+').

like image 27
mabead Avatar answered Sep 27 '22 03:09

mabead