Can't get basic Powershell script running inside Team City

删除回忆录丶 提交于 2019-11-27 17:56:58

问题


Here's my configuration:

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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9165658/cant-get-basic-powershell-script-running-inside-team-city

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!