5

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.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
mabead
  • 2,171
  • 2
  • 27
  • 42

3 Answers3

9

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

For example:

Write-Host "##vso[task.logissue type=warning;]Test warning"
Write-Host "##vso[task.logissue type=error;]Test error"
Matt Brooks
  • 1,584
  • 1
  • 14
  • 27
  • 3
    It is important to note that adding errors through logging commands does not mark the build step as failed. One must therefore throw an exception (or log on stderr) when it logged at least one error through logging commands. – mabead Apr 04 '16 at 13:47
  • Good point regarding failing the build step. I've corrected a typo in the first URL now. – Matt Brooks Apr 04 '16 at 14:24
  • That is what i need, but using TFS 2013. Is there any way to achieve it with 2013? – lstanczyk Aug 31 '17 at 17:41
1

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 '+').

mabead
  • 2,171
  • 2
  • 27
  • 42
0

In a build with Visual Studio 2013, I have tested with these two approaches and worked:

[Console]::Error.WriteLine("An error occurred.")

$host.ui.WriteErrorLine("Other way to show an error.")

Hope that helps.