Zergatul
Zergatul

Reputation: 2010

Pass multiline parameter to powershell script

I have a TeamCity build with prompt text parameter, and I pass it into powershell script (by using PowerShell runner, with script file and execution mode "Execute .ps1 from external file"). Script argument looks like below:

-MyParameter1 "%myparameter1%"
-MyParameter2 "%myparameter2%"

Build initiating user can enter multiline parameter. Example below:

multiline test
second line
3rd line

And in this case I am getting an error:

A positional parameter cannot be found that accepts argument 'test second line 3rd line'.

In the TeamCity log I am seeing next command:

Command: C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
PowerShell arguments: -NoProfile, -NonInteractive, -ExecutionPolicy, ByPass, -File, C:\BuildAgent\work\***\MyScript.ps1, -MyParameter1, "1578", -MyParameter2, "multiline test, second, line, 3rd, line"

Parameter definition in Script.ps1:

[Parameter(Mandatory=$true)]
[String]$MyParameter2,

TeamCity version: 2018.1.1

What can I do to pass multiline parameter to powershell script?

Upvotes: 1

Views: 3245

Answers (1)

Zergatul
Zergatul

Reputation: 2010

Possible solution. Before executing necessary PowerShell script, add step with inline PowerShell in TeamCity:

$param2 = @'
%myparameter2%
'@
$param2 = [System.Text.Encoding]::UTF8.GetBytes($param2)
$param2 = [System.Convert]::ToBase64String($param2)
Write-Output "##teamcity[setParameter name='myparameter2' value='$param2']"

This script will replace build parameter myparameter2 with its base64 representation. You can pass it as usual parameter to script, and then do base64 decode.

Upvotes: 1

Related Questions