从PowerShell脚本中提供退出代码时,TFS 2013 Build Agent无法返回正确的值 [英] TFS 2013 Build Agent fails to return correct value when given an exit code from a PowerShell Script

查看:64
本文介绍了从PowerShell脚本中提供退出代码时,TFS 2013 Build Agent无法返回正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为调用批处理文件的POC设置了一个门禁的签入构建过程。该批处理文件依次调用PowerShell脚本,该脚本将执行一些有用的功能,然后返回零(函数成功)或非零值(函数失败)。
此值将传递回批处理文件,然后批处理文件将通知构建代理程序已经传递了构建并且代码可以签入,或者构建失败并且不应该检入代码。 / p>

从命令行运行时,或者在PS控制台中调用PS1时,文件按预期工作。然而,构建代理要么没有从测试中获得预期值,要么忽略该值。


在我上次测试运行中,确认批处理文件调用/ PS1脚本之后工作,我在构建日志中找到以下内容:


序列

在编译逻辑块中:

在编译逻辑中块:E:\ws\1\426 \Sources

输出:运行单元测试...



00:00
$
序列

在编译逻辑块中:

在编译逻辑块中:E:\ws \\\\ 426 \Sources

输出:退出代码:-196608

最终物业价值

参数= / c E:\ws\1\426\Sources\common\ServerFramework\Build-n-test.bat E:\ws\1\426 \Sources  2.2 .222.42 1_GatedCI_Branch_Component

EnvironmentVariables = 

FileName = cmd.exe

OutputEncoding = System.Text.SBCSCodePageEn编码

结果= 0

WorkingDirectory = E:\ws\1\426 \Sources

最终物业价值
条件=真


返回的退出代码应为零或1.而是我们得到-196608。我怀疑这是一个背景问题;任何帮助将不胜感激。


入口点批处理文件如下所示:

 @ Echo  off 
Echo 运行 单元测试  ...
Powershell.exe  -executionpolicy  remotesigned  -File  ./ Parsing_Script .ps1
Echo 退出代码: %errorlevel%

PS1脚本包含:

  $ input_path    =   'gtest_output_Passestask.txt' 
$ regex   =   'FAILED' 
$ result   =   & ;   select-string   - 路径  $ input_path   - 模式  $ regex   - AllMatches   |   { $ _ 匹配 }   |   { $ _ }
if   $ result   - eq   'FAILED' < span style ="color:whitesmoke">)
{
     退出  1  
}
else
{
     退出  0
}

其中" gtest_output_PassesTest.txt"是用于设置退出值的存根。


解决方案

使用-file调用powershell时出错。它将始终返回0


您可以尝试使用写入错误/写入警告或尝试此操作:

 if( 


result -eq'FAILED')
{
[System.Environment] :: Exit(1)
}
else
{
[System.Environment] ::退出(0)
}


We have a gated check-in build process set up for a POC that calls a batch file. This batch file in turn calls a PowerShell script that will do some useful function, and then return either a zero (function succeeded) or a non-zero value (function failed). This value is passed back to the batch file, which will then notify the build agent that the build has either passed and the code can be checked in, or the build has failed and the code should NOT be checked in.

When run from a command line, or when the PS1 is invoked in a PS console, the files work as expected. Yet, the build agent is either not getting back the expected value from the test, or is ignoring the value.

In my last test run, after confirming that the batch file call/PS1 script were working, I found the following in the build log:

Sequence
In the compile Logic block:
In the compile Logic block: E:\ws\1\426\Sources
output: Running unit tests ...

00:00
Sequence
In the compile Logic block:
In the compile Logic block: E:\ws\1\426\Sources
output: Exit code: -196608
Final Property Values
Arguments = /c E:\ws\1\426\Sources\common\ServerFramework\Build-n-test.bat E:\ws\1\426\Sources  2.2.222.42 1_GatedCI_Branch_Component
EnvironmentVariables = 
FileName = cmd.exe
OutputEncoding = System.Text.SBCSCodePageEncoding
Result = 0
WorkingDirectory = E:\ws\1\426\Sources
Final Property Values
Condition = True

The exit code the is returned should be either zero or 1. Instead we are getting -196608. I suspect this is a context issue; any assistance would be greatly appreciated.

The entry point batch file looks like this:

@Echo off
Echo Running unit tests ...
Powershell.exe -executionpolicy remotesigned -File ./Parsing_Script.ps1
Echo Exit code: %errorlevel%

The PS1 script has this:

$input_path = 'gtest_output_Passestask.txt'
$regex = 'FAILED' 
$result = & select-string -Path $input_path -Pattern $regex -AllMatches | %{$_.Matches} | %{$_.Value}
if ($result -eq 'FAILED')
{
    exit 1 
}
else
{
    exit 0
}

where "gtest_output_PassesTest.txt" is a stub used to set the exit value.

解决方案

There is an error in powershell when called with -file. it will always return 0

You can try use either write-error/write-warning or try this:

if (


result -eq 'FAILED') { [System.Environment]::Exit(1) } else { [System.Environment]::Exit(0) }


这篇关于从PowerShell脚本中提供退出代码时,TFS 2013 Build Agent无法返回正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆