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.



There's 6 Comments So Far
April 13th, 2011 at 20:04
UTF-8 unicode with/without signature is pain in the ass. Especially http://goo.gl/adzFM
April 13th, 2011 at 20:31
Yes.
Though I’m using UTF-8 LE in this case.
July 20th, 2011 at 22:21
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?
July 21st, 2011 at 10:26
It’s using “Start-Process sqlcmd -workingDirectory -argumentList -wait -WindowStyle Hidden”
July 21st, 2011 at 17:25
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?
July 21st, 2011 at 17:35
I don’t know whether I had to, but it solved my problem.
Share your thoughts, leave a comment!