How to set a PowerShell switch parameter from TeamCity build configuration How to set a PowerShell switch parameter from TeamCity build configuration powershell powershell

How to set a PowerShell switch parameter from TeamCity build configuration


This is an old question, but if you are still looking for an answer, I managed to get it working.

The main trick is that you shouldn't actually use the colon between the parameter name and the value as you would with PowerShell (I know, confusing...).

It looks like TeamCity is invoking the script in a different way and passes the parameters as string.

So with your example, the following should work:-preRelease $%IncludePreRelease%

Note that I have added a dollar sign in front of the TeamCity variable to change "true" into "$true"

Let me know if it works for you

Thanks!


No, this still won't work. It seems that TC will always treat the value as a string no matter what. Maybe the answer was from a version of TC that allowed this, but the latest release does not.

Without colon:

[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'.

With colon:

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

Removing the [switch] also does not work as any value you give it will not be treated as a Boolean - but rather a string assigned to a Boolean - and, thus, all Boolean operations will be $true.

In other words, anything you send will look like:

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

(none of which are == $false but all of which are equivalent to $true)

I have no idea how to get TC to accept Booleans!Best I could do was assume the argument was a string and use System.Convert.ToBoolean() internally in my script...


According to the Microsoft .Net documentation for Convert.ToBoolean the value passed to the method must be:

For a successful conversion to occur, the value parameter must equal either Boolean.TrueString, a constant whose value is True, Boolean.FalseString, a constant whose value is False, or it must be null. In comparing value with Boolean.TrueString and Boolean.FalseString, the method ignores case as well as leading and trailing white space.

If you change your TeamCity IncludePreRelease variable value to either "True" or "False" (without the quotes) then it should convert correctly.