How to fail the build from a PowerShell task in TFS 2015

I’ve created a very simple script:

 Write-Error ("Some error")
 exit 1

Save this as a PowerShell script. Create a new Empty Build definition and only add one PowerShell task that points to your script. When I do this I get a failed build with the following error:

2015-12-30T10:27:29.4872452Z . 'C:\a\1\s\script.ps1' 
2015-12-30T10:27:29.6780242Z Executing the following powershell script. (workingFolder = C:\a\1\s)
2015-12-30T10:27:29.6790500Z C:\a\1\s\script.ps1 
2015-12-30T10:27:33.8017820Z ##[error]C:\a\1\s\script.ps1 : Some error
2015-12-30T10:27:33.8027833Z ##[error]At line:1 char:1
2015-12-30T10:27:33.8037819Z ##[error]+ . 'C:\a\1\s\script.ps1'
2015-12-30T10:27:33.8037819Z ##[error]+ ~~~~~~~~~~~~~~~~~~~~~~~
2015-12-30T10:27:33.8047816Z ##[error]    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
2015-12-30T10:27:33.8047816Z ##[error]    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,script.ps1
2015-12-30T10:27:33.8057887Z ##[error] 
2015-12-30T10:27:33.8057887Z ##[error]Process completed with exit code 1 and had 1 error(s) written to the error stream.

The main difference with your script is the usage of Write-Error in combination with an exit 1.

Leave a Comment