gpt4 book ai didi

powershell - 为什么我的 PowerShell 退出代码始终为 "0"?

转载 作者:行者123 更新时间:2023-12-03 05:22:40 24 4
gpt4 key购买 nike

我有一个 PowerShell 脚本,如下

##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']

据我所知,an uncaught Throw将导致退出代码为 1,但不幸的是 TeamCity 另有说法。

[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

还需要注意的是,我的执行模式设置为使用“-File”参数执行.ps1脚本

我尝试将其更改为使用“-Command -”参数将脚本放入 PowerShell 标准输入,但即使通过了测试,它也会失败,退出代码为 1。我确信将其作为 -File 运行将是正确的方法。

如果我打开位于 C:\BuildAgent\work\e903de7564e599c8\build.ps1 的脚本并在 CMD 中手动运行它,它会执行相同的操作...即失败的测试失败,%errorlevel% 仍为 0

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

最佳答案

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

(更新:下面的链接不再有效。请在 PowerShell: Hot (1454 ideas) – Windows Server 上查找或报告此问题)

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

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

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

关于powershell - 为什么我的 PowerShell 退出代码始终为 "0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com