4

我有一个试图通过 teamcity 运行的 powershell 脚本。该脚本存储在构建代码的 git 存储库中。我已将 teamcity 设置为将脚本作为脚本文件运行

如果我从构建服务器上的 powershell 命令行运行脚本,它会按预期运行,如果我从 teamcity 运行相同的脚本,我会收到类似的错误

[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]  

为什么会发生这种情况?

4

2 回答 2

3

解决方案比它应该的要简单得多。teamcity 中有一个名为“脚本执行模式”的选项,我将其更改Put script into PowerShell stdin with "-Command -" argumentsExecute .ps1 script with "-File" argument一旦完成,脚本开始按预期运行。

于 2013-05-07T15:01:23.710 回答
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.

于 2015-04-16T16:27:41.610 回答