I am trying to make a certain result in a PowerShell script fail the build process, but its not working for me. I am using the new build actions from TFS 2015 and tried the following options:
- Logging commands
- exit 1
I do get red text in the log window of the step as well as marked 'Issues' in the build overview, but the build result is still Green: 'Build succeeded'
I want to use the failure in the script to fail the build and thus send an email using an alert on failed builds.
Edit: including the PS script:
Param(
[string]$url
)
if ($url -eq '')
{
#use default value
$url = 'https://myurl.com'
}
$req = [system.Net.WebRequest]::Create($url)
$req.Timeout = 60000 * 5 # = 5 minutes
try
{
Write-Host "Try to get response from url:" $url
$res = $req.GetResponse()
Write-Host "Closing the connection"
$req.Close() # close the connection
}
catch [System.Net.WebException]
{
Write-Host "Got an exception"
Write-Host "##vso[task.logissue type=error;]Exception: " $_.Exception
if ($_.response) # try to close the connection
{
$_.response.Close();
}
$res = $_.Exception.Response
}
$printCode=[int]$res.StatusCode
Write-Host "Result StatusCode:" $res.StatusCode "(" $printCode ")"
If ([int]$res.StatusCode -eq 200)
{
Write-Host "##vso[task.complete result=Succeeded;]Done"
}
Else
{
Write-Host "##vso[task.logissue type=error;]Test error: " $res
Write-Host "##vso[task.complete result=Failed;]Error testing if demo site is up"
exit 1
}