简体   繁体   English

如何使TFS 2015中的PowerShell任务失败

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

I am trying to make a certain result in a PowerShell script fail the build process, but its not working for me. 我试图在PowerShell脚本中使某个结果在构建过程中失败,但它对我不起作用。 I am using the new build actions from TFS 2015 and tried the following options: 我正在使用TFS 2015中的新构建操作并尝试以下选项:

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: 编辑:包括PS脚本:

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
}

I've created a very simple script: 我创建了一个非常简单的脚本:

 Write-Error ("Some error")
 exit 1

Save this as a PowerShell script. 将其另存为PowerShell脚本。 Create a new Empty Build definition and only add one PowerShell task that points to your script. 创建一个新的Empty Build定义,只添加一个指向脚本的PowerShell任务。 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. 与脚本的主要区别在于Write-Error与exit 1的结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代理身份验证问题:Azure Powershell构建任务内部部署TFS服务器2015 - Proxy authentication issue: Azure Powershell Build Task on premise TFS server 2015 使用PowerShell在TFS 2015 REST API中构建队列 - Queue Build in TFS 2015 REST API with PowerShell 如何在tfs 2015中读取目标机器任务上Powershell中特殊字符,空格等会话变量 - how to read the session variables with special chars, spaces ect in powershell on target machine task in tfs 2015 vNext TFS 2015在PowerShell中的QueryBuildDefinition函数找不到我的构建定义 - vNext TFS 2015 QueryBuildDefinition function in powershell not finding my build definition 无法加载模块在TFS 2015构建中运行PowerShell脚本时出错 - Module cannot be loaded error running PowerShell script in TFS 2015 build 如何从TFS构建过程将参数传递给PowerShell脚本? - How to pass parameters to PowerShell script from TFS build process? 如何将参数从 powershell 脚本传递到 TFS 2013 构建? - How to pass parameters from a powershell script to TFS 2013 build? 如何从Powershell脚本中向TFS vNext报告构建进度? - How to report build progress to TFS vNext from within a powershell script? 如何使用Powershell在NUnitTest上构建失败 - How to Fail Build on NUnitTest Fail using Powershell 如何通过Powershell使构建失败 - How to fail the build via powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM