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

时间:2015-12-28 22:48:18

标签: powershell tfs2015

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

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

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

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

1 个答案:

答案 0 :(得分:18)

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

 NSDictionary *bodyDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bodyDictionary options:0 error:nil];

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    body = [NSString stringWithFormat:@"message=%@",jsonString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
 [request setTimeoutInterval:300]; 
    [request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

    [connection start];

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

 Write-Error ("Some error")
 exit 1

与脚本的主要区别在于Write-Error与退出1的结合使用。