4

I have a powershell script that I am trying to run via teamcity. The script is stored in the git repo that the code is built from. I have set teamcity to run the script as a script file

If I run the script from a powershell commandline on the build sever it runs as expected, if I run the same script from teamcity I get errors like

[16:04:25][Step 3/3] Get-Date : Cannot bind parameter 'Date'. Cannot convert value "–f" to type 
[16:04:25][Step 3/3] "System.DateTime". Error: "The string was not recognized as a valid DateTime. 
[16:04:25][Step 3/3] There is an unknown word starting at index 0."
[16:04:25][Step 3/3] At line:1 char:26
[16:04:25][Step 3/3] + Write-Output "$(Get-Date –f $timeStampFormat) - Upgrading Deployment: In 
[16:04:25][Step 3/3] progr ...
[16:04:25][Step 3/3] +                          ~~~~
[16:04:25][Step 3/3]     + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindin 
[16:04:25][Step 3/3]    gException
[16:04:25][Step 3/3]     + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh 
[16:04:25][Step 3/3]    ell.Commands.GetDateCommand
[16:04:25][Step 3/3]  

Why might this be happening?

ilivewithian
  • 19,476
  • 19
  • 103
  • 165
  • 1
    difficult to pinpoint the problem, but there's a possibility that the Powershell script encoding is been changed when it goes into the git repo thus changing some of the characters in the script. Could you try to output the line that's causing the error before it executes and see what the values are? – Musaab Al-Okaidi May 01 '13 at 20:24
  • try and run the powershell script for the location on the agent and also check the encoding like Muz says, Git and file coding for sure. – James Woolfenden May 02 '13 at 08:46

2 Answers2

3

The solution was much simpler that it should have been. There is an option in teamcity called "Script execution mode", which I changed from Put script into PowerShell stdin with "-Command -" arguments to Execute .ps1 script with "-File" argument once that was done the script started running as expected.

ilivewithian
  • 19,476
  • 19
  • 103
  • 165
0

I ran into a similar issue, but the encoding of the script itself wasn't the issue, it was the output of the script. I was using PowerShell to read in an app.config file, replace a variable within the file, and then write the file back out to its original path. The script was receiving an app.config in UTF-8 encoding, but was outputting a file in USC-2 encoding. Msbuild threw an error when it received unexpected characters from the app.config.

The script was writing the new output using $updatedConfig > $path shorthand for out-file. I had to replace it with the full command $updatedConfig | out-file -FilePath $path -encoding utf8 in order to specify the output encoding.

Note: I am successfully using the shorthand snippet above in all but two TeamCity build configurations. I checked and double-checked that the original config files are coming from source control with the proper encoding, even going so far as to convert the files to UTF-8 with BOM. Nothing worked except removing the shorthand syntax.

Ben
  • 308
  • 2
  • 13