success when executing failing powershell script with -file

When executing powershell scripts with "-file", errors are reported as success.

I have this powershell script:
$ErrorActionPreference = "Stop"
function Main()
{
    Write-Host "1"
    ren locked locked2
    Write-Host "2"
}
Main

...which throws exception on the rename command (because the folder is locked by windows - any application might do this, i'm using cmd.exe to provoke an error in this case).

I've currently setup the powershell build step to be executed with "execute .ps1 script with -file argument" instead of "-command -", because -file is preventing another problem - mangling of whitespace in the pipe from stdin.

The script above does throw an exception, but this is reported as a Success by teamcity. This is a bug I suppose? Is there any work-a-round? (without going back to using -command/stdin)

Thanks.

3 comments
Comment actions Permalink

Hi

It looks similar to issue TW-21554.
It should be fixed in 7.1. Also you can download updated plugin for previous versions there.

0
Comment actions Permalink

7.1 (build 23907) didn't fix this. Is there something else I should do?

The build log indicates that the "Process exited with code 0" when the script fails.

0
Comment actions Permalink

I believe the problem is that the TeamCity command is using && operator for returning error code. Thus the exit /b command only runs when the ps.1 call is successful. The & operator should be used (http://support.microsoft.com/kb/279253).

From log (TC v7.0):
     cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File D:\...\powershell1512375851741608244.ps1 && exit /b %ERRORLEVEL%

You should be able to work around this by using the command shell build task instead of Powershell, but you will lose the ability to enter your PSH script within TeamCity.

0

Please sign in to leave a comment.