TeamCity, PowerShell and sqlcmd problem (and solution)

In my current project we’re using TeamCity as a continuous integration server and psake to run all out build and deployment tasks. Part of the deployment is execution of SQL scripts to create database and create structures in it. And as a heavy-duty console user, I’m using sqlcmd to do all my work with database. So I simply called sqlcmd with according parameters and to execute the scripts. Sadly for some strange reason, the PowerShell runner in TeamCity kept running in a loop eventually ending with timeout. Even worse, running it locally directly in PowerShell was fine. After small research done by my colleague we found the reason is sqlcmd.

The quick’n'dirty solution was to run it using Start-Process, sadly we lost the output (using -noNewWindow resulted in same problem), so any error was about guessing. Simple techniques, like redirecting output from cmdlet ended with same problem.

But I actually found a solution. I redirected the output from sqlcmd directly with -o switch and used Unicode using -u. Then I echoed the file via Get-Content cmdlet with -encoding Unicode switch.

After I rigorously specified the encodings for output and input, everything started running fine. Probably there was some problem with BOM being in input, causing headache to the runner.

Never mind, now it’s fine, and I’m happy, because I see errors in processing SQL scripts eventually.

8 thoughts on “TeamCity, PowerShell and sqlcmd problem (and solution)

  1. cincura.net Post author

    Yes.

    Though I’m using UTF-8 LE in this case.

  2. Mark Boltuc

    I’m running into this same issue with TeamCity 6.5.2. However, I can’t seem to get the right combo to make it work. Can you send me or post a code snippet of what your psake task looks like?

  3. cincura.net Post author

    It’s using “Start-Process sqlcmd -workingDirectory -argumentList -wait -WindowStyle Hidden”

  4. Mark Boltuc

    Hmm, I couldn’t seem to get that working either. I ended up having to split my database stuff out into a seperate build step that uses the command line runner with a custom script: .\packages\psake.4.0.1.0\tools\psake.cmd -TaskList DbReset

    Did you have to use Start-Process + all the crazy unicode output stuff?

  5. cincura.net Post author

    I don’t know whether I had to, but it solved my problem. :)

  6. cincura.net Post author

    I don’t have the code. But simply starting new process, redirecting output to file and then optionally reading and flushing it back.

Leave a Reply