13

Here's my configuration: enter image description here

On the build log, I only see the output of the first two lines, and then "Process exited with code 0" as the last output of this build step.

I tried opening a terminal in the build server in the SYSTEM account (using PsTools), since Team City is configured to run under said account. Then, I created a Test.ps1 file with the same content and ran a command just like Team City's:

[Step 1/4] Starting: C:\Windows\system32\cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -Command - <C:\TeamCity\buildAgent\temp\buildTmp\powershell5129275380148486045.ps1 && exit /b %ERRORLEVEL%

(except for the path to the .ps1 file and the cmd.exe initial part, of course). I saw the output of the two first lines, and then the terminal disappeared all of a sudden!

Where did I mess up? I'm new to Powershell, by the way.

dario_ramos
  • 7,118
  • 9
  • 61
  • 108
  • Have you set the Execution Policy to allow your script? – ccellar Feb 06 '12 at 19:10
  • 1
    I chose "Put script into Powershell stdin with -Command arguments" to avoid that. Besides, if it can run the first two lines, I'd think permissions are OK, or do I need extra permissions to run conditional logic? That would be quite crazy :P – dario_ramos Feb 06 '12 at 19:18
  • 1
    I have no context for this tool, but I wonder if it's having issues with multiline stdin? Try replacing conditional with the following (one-liner) to see if you get different results: if ("1" -eq "1") { write-host "test" } else { write-host "else test" } – Daniel Richnak Feb 07 '12 at 00:23
  • 1
    When I try that script with a similar command line, it all works fine. Try replacing the `&& exit ...` with `&& pause` to see if you can see the last the "Ok, nothing to see..." line before the window closes. Or you could just change the `/c` to `/k` so the window will remain - just to help debug this. – Keith Hill Feb 07 '12 at 02:20
  • 3
    I think your script is too hilarious, try a less hilarious script :-) – Andy Arismendi Feb 07 '12 at 02:31

3 Answers3

19

The stdin command option of Powershell has some weirdness around multiline commands like that.

You script in the following form would work:

write-host "test"
write-host "test2"
if("1" -eq "1"){write-host "test3 in if"} else {write-host "test4 in else"}

The ideal way would be to use the Script : File option in TeamCity which will will run the script you specify using the -File parameter to Powershell.

If you don't want to have a file and having VCS, in the current setup, change Script Execution Mode to Execute .ps1 file with -File argument.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • You're right, if the `if-else` clauses are one-liners, it works. Wonder if this also happens with Powershell 2.0? Anyway, since code readability is priority, I decided to follow your second suggestion (which implied opening a terminal under the SYSTEM account, and running the following commands: 1) `powershell` 2) `Set-ExecutionPolicy RemoteSigned` 3) `exit`) – dario_ramos Feb 07 '12 at 14:33
  • 2
    @dario_ramos - Happens upto powershell 3.0 CTP :) – manojlds Feb 08 '12 at 04:37
  • Note that you can use ";" to separate multiple statements on a single line. – Dunc Sep 06 '12 at 10:24
  • And the answer is still valid... unfortunately – Rober Feb 12 '18 at 12:59
3

I've had this problem with inline powershell scripts with TeamCity (right up until the current version of 7.1.3). I've found the problem to be the tab character rather than multi-line statements. Try replacing the tab characters with spaces (while still remaining multi-line) and the script should run fine.

Mark Glasgow
  • 529
  • 2
  • 9
1

You could try putting the brace opening the block on the same line as the If.

I.e.,

If ('1' -eq '1') {
    ...
}
Else {
    ...
}

That's the usual styling you see with Powershell, and obviously, putting the braces on the next line can cause problems.

jpmc26
  • 28,463
  • 14
  • 94
  • 146