如何从TeamCity构建配置中设置PowerShell开关参数

Kla*_*urn 10 powershell teamcity parameter-passing

我有一个PowerShell脚本,带有一个switch(boolean)参数,我想从TeamCity构建步骤中调用它.

我希望根据TeamCity构建参数(配置参数)设置switch的值(true/false).

所以,像这样: 在此输入图像描述

在PowerShell runner的构建步骤中: 在此输入图像描述

但上述方法无效.

我收到这个错误

[14:27:01][Step 1/1] Cannot process argument transformation on parameter 'preRelease'. Cannot 
[14:27:01][Step 1/1] convert value "System.String" to type 
[14:27:01][Step 1/1] "System.Management.Automation.SwitchParameter". Boolean parameters accept only 
[14:27:01][Step 1/1] Boolean values and numbers, such as $True, $False, 1 or 0.
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,似乎PowerShell坚持将参数解释为字符串.

我尝试过编写脚本参数的许多变体.这些都不起作用:

-preRelease:%IncludePreRelease%
-preRelease:([boolean]%IncludePreRelease%)
-preRelease:([System.Convert]::ToBoolean(%IncludePreRelease%))
Run Code Online (Sandbox Code Playgroud)

小智 6

不,这仍然行不通.无论如何,TC似乎总是将值视为字符串.也许答案来自允许这种情况的TC版本,但最新版本没有.

没有冒号:

[16:18:37]Step 1/6: Migrate Up (Powershell)
[16:18:37][Step 1/6] PowerShell Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
[16:18:37][Step 1/6] PowerShell arguments: [-NonInteractive, -ExecutionPolicy, ByPass, -File, C:\BuildAgent\work\f2797fec10821a01\data\Migration\MigrateUp.ps1, "data\change scripts", DB, xxxxxxx, sa, *******, -NoExec, $false]
[16:18:37][Step 1/6] C:\BuildAgent\work\f2797fec10821a01\data\Migration\MigrateUp.ps1 : A positional parameter cannot be found that accepts argument '$false'.
Run Code Online (Sandbox Code Playgroud)

用冒号:

 [16:18:37]Step 2/6: Migrate Up (Powershell)
 [16:18:37][Step 2/6] PowerShell Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
 [16:18:37][Step 2/6] PowerShell arguments: [-NonInteractive, -ExecutionPolicy, ByPass, -File, C:\BuildAgent\work\f2797fec10821a01\data\Migration\MigrateUp.ps1, "data\change scripts", DB, xxxxxx, sa, *******, -NoExec:$false]
 [16:18:37][Step 2/6] C:\BuildAgent\work\f2797fec10821a01\data\Migration\MigrateUp.ps1 : Cannot process argument transformation on parameter 'NoExec'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.
Run Code Online (Sandbox Code Playgroud)

删除[switch]也不起作用,因为你给它的任何值都不会被视为布尔值 - 而是一个分配给布尔值的字符串 - 因此,所有布尔运算都将是$true.

换句话说,您发送的任何内容都将如下所示:

  • 0 =>'0'
  • 1 =>'1'
  • $ false =>'$ false'
  • $ true =>'$ true'

(没有一个是== $false但所有这些都等于$ true)

我不知道如何让TC接受布尔人!我能做的最好的事情是假设参数是一个字符串,并在我的脚本内部使用System.Convert.ToBoolean()...


Fab*_*fin 5

这是一个老问题,但如果您仍在寻找答案,我设法让它运转起来.

主要技巧是你不应该像使用PowerShell一样在参数名和值之间实际使用冒号(我知道,令人困惑......).

看起来TeamCity以不同的方式调用脚本并将参数作为字符串传递.

因此,对于您的示例,以下内容应该有效:-preRelease $%IncludePreRelease%

请注意,我在TeamCity变量前面添加了一个美元符号,将"true"更改为"$ true"

请让我知道这对你有没有用

谢谢!


小智 1

根据Convert.ToBoolean的 Microsoft .Net 文档,传递给该方法的值必须是:

为了成功进行转换,value 参数必须等于 Boolean.TrueString(值为 True 的常量)、Boolean.FalseString(值为 False 的常量),或者必须为 null。在将值与 Boolean.TrueString 和 Boolean.FalseString 进行比较时,该方法会忽略大小写以及前导和尾随空格。

如果将 TeamCity IncludePreRelease变量值更改为“ True ”或“ False ”(不带引号),那么它应该正确转换。