为什么我的 PowerShell 退出代码总是“0"? [英] Why are my PowerShell exit codes always "0"?

查看:67
本文介绍了为什么我的 PowerShell 退出代码总是“0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 脚本如下

I've got a PowerShell script as follows

##teamcity[progressMessage 'Beginning build']
# If the build computer is not running the appropriate version of .NET, then the build will not run. Throw an error immediately.
if( (ls "$env:windir\Microsoft.NET\Framework\v4.0*") -eq $null ) {
    throw "This project requires .NET 4.0 to compile. Unfortunately .NET 4.0 doesn't appear to be installed on this machine."
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Setting up variables']
# Set up variables for the build script
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$v4_net_version = (ls "$env:windir\Microsoft.NET\Framework\v4.0*").Name
$nl = [Environment]::NewLine

Copy-Item -LiteralPath "$directorypath\packages\NUnit.2.6.2\lib\nunit.framework.dll" "$directorypath\Pandell.Tests\bin\debug" -Force

##teamcity[progressMessage 'Using msbuild.exe to build the project']
# Build the project using msbuild.exe.
# Note we've already determined that .NET is already installed on this computer.
cmd /c C:\Windows\Microsoft.NET\Framework\$v4_net_version\msbuild.exe "$directorypath\Pandell.sln" /p:Configuration=Release
cmd /c C:\Windows\Microsoft.NET\Framework\$v4_net_version\msbuild.exe "$directorypath\Pandell.sln" /p:Configuration=Debug

# Break if the build throws an error.
if(! $?) {
    throw "Fatal error, project build failed"
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Build Passed']
# Good, the build passed
Write-Host "$nl project build passed."  -ForegroundColor Green


##teamcity[progressMessage 'running tests']
# Run the tests.
cmd /c $directorypath\build_tools\nunit\nunit-console.exe $directorypath\Pandell.Tests\bin\debug\Pandell.Tests.dll

# Break if the tests throw an error.
if(! $?) {
    throw "Test run failed."
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Tests passed']

据我所知,未捕获的 Throw 将导致退出代码 1,但不幸的是 TeamCity 另有说法.

From what I'm lead to believe, an uncaught Throw will result in an exit code of 1, but unfortunately TeamCity is saying otherwise.

[19:32:20]Test run failed.
[19:32:20]At C:\BuildAgent\work\e903de7564e599c8\build.ps1:44 char:2
[19:32:20]+     throw "Test run failed."
[19:32:20]+     ~~~~~~~~~~~~~~~~~~~~~~~~
[19:32:20]    + CategoryInfo          : OperationStopped: (Test run failed.:String) [],
[19:32:20]   RuntimeException
[19:32:20]    + FullyQualifiedErrorId : Test run failed.
[19:32:20]
[19:32:20]Process exited with code 0
[19:32:20]Publishing internal artifacts
[19:32:20][Publishing internal artifacts] Sending build.finish.properties.gz file
[19:32:20]Build finished

需要注意的是,我的 Execution Mode 设置为 Execute .ps1 script with "-File" argument.

It might also be important to note that my Execution Mode is set to Execute .ps1 script with "-File" argument.

我尝试将其更改为 Put script into PowerShell stdin with "-Command -" arguments,但是即使通过了测试,它也失败了,退出代码为 1.我确信将它作为 -File 运行将是正确的方式.

I tried changing it to Put script into PowerShell stdin with "-Command -" arguments, but then it failed with an exit code of 1 even with passing tests. I'm sure that running it as -File is going to be the right way.

如果我打开位于 C:\BuildAgent\work\e903de7564e599c8\build.ps1 的脚本并在 CMD 中手动运行它,它会做同样的事情......即失败的测试失败,并且 %errorlevel% 仍然是 0.

If I open up the script located at C:\BuildAgent\work\e903de7564e599c8\build.ps1 and run it manually in CMD, it does the same thing... I.e., the failing tests fail, and the %errorlevel% is still 0.

然而,如果我在 PowerShell 中运行它并调用 $LASTEXITCODE,它每次都会返回正确的代码.

Yet, if I run it in PowerShell and call $LASTEXITCODE, it returns the right code every time.

推荐答案

这是 PowerShell 的一个已知问题.使用 -file 执行脚本会在不应该返回 0 的退出代码.

This is a known issue with PowerShell. Executing a script with -file returns an exit code of 0 when it shouldn't.

(更新:以下链接不再有效.请在PowerShell:热门(1454 个想法)– Windows Server)

(Update: The links below no longer work. Please look for, or report, this problem on PowerShell: Hot (1454 ideas) – Windows Server)

https://connect.microsoft.com/PowerShell/feedback/details/750653/powershell-exe-doesn-t-return-correct-exit-codes-when-using-the-file-option

由于使用 -command 对您不起作用,您可以尝试在脚本顶部添加陷阱:

Since using -command wasn't working for you, you could try adding a trap at the top of the script:

trap
{
    write-output $_
    ##teamcity[buildStatus status='FAILURE' ]
    exit 1
}

当抛出异常时,上面应该会产生一个正确的退出代码.

The above should result in a proper exit code when an exception is thrown.

这篇关于为什么我的 PowerShell 退出代码总是“0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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