当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何使TFS2015中的PowerShell任务失败》经验,为你挑选了1个好方法。

我试图在PowerShell脚本中使某个结果在构建过程中失败,但它对我不起作用.我正在使用TFS 2015中的新构建操作并尝试以下选项:

记录命令

退出1

我确实在步骤的日志窗口中获得了红色文本,并在构建概述中标记了"问题",但构建结果仍为绿色:"构建成功"

我想使用脚本中的失败来使构建失败,从而使用失败构建的警报发送电子邮件.

编辑:包括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
}

Wouter de Ko.. 20

我创建了一个非常简单的脚本:

 Write-Error ("Some error")
 exit 1

将其另存为PowerShell脚本.创建一个新的Empty Build定义,只添加一个指向脚本的PowerShell任务.当我这样做时,我得到一个失败的构建与以下错误:

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.

与脚本的主要区别在于Write-Error与exit 1的结合使用.



1> Wouter de Ko..:

我创建了一个非常简单的脚本:

 Write-Error ("Some error")
 exit 1

将其另存为PowerShell脚本.创建一个新的Empty Build定义,只添加一个指向脚本的PowerShell任务.当我这样做时,我得到一个失败的构建与以下错误:

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.

与脚本的主要区别在于Write-Error与exit 1的结合使用.


@ DavidV.Corbin Rob要求从PowerShell脚本中失败构建.失败的唯一原因是从脚本返回退出代码并将其推送到日志.或者你不想记录你的错误吗?
推荐阅读
大大炮
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有