Pass array of variables into underlying powershell script

In my standalone Powershell scripts I can do something like
[object[]] $values = “Text”, “More Text”, 70, 1, “Even More Text”

To pass in 2 strings, a couple of numerics and a final string.

Question is, how can I replicate this in my step template using a parameter. If I place “Text”, “More Text”, 70, 1, “Even More Text” as a parameter value, then I get one single entry in the underlying array, as opposed to the expected 5 values.

Hi Rachel,

Thanks for getting in touch! I’m sorry it’s not working as expected. The reason why it’s giving you one single entry instead of the 5 expected is because we treat everything as text. What I’d suggest with your PowerShell script is to pass them as separate parameters, naming them accordingly.

Another option could be to use the PowerShell split method. A good example of this is laid out in the following thread (however, even if you have a number in a variable, you’d have to parse it).
http://help.octopusdeploy.com/discussions/questions/1557-passing-an-array-as-a-variable-to-predeploy-script

I hope this helps! Let me know if you have any further questions.

Best regards,

Kenny

A split is what I’m actually doing here which kinda works, but I lose the ability to have maintain datatype resolution as a result (e.g. all my integers are seen a strings). Multiple parameters are not an option because the step template is used to call multiple stored procedures in Sql Server, each with different parameters.

There a reason why Octopus doesn’t treat PowerShell scripts are first class citizens and thus allowing correct parameter passing etc.?

Hi Rachel,

Thanks for following up. I can understand why you’d need to keep things as different data types in the array. I’d say that PowerShell is treated as a first class citizen basically everywhere in Octopus. But in the case of step parameters, because of the way the values need to be sent through the API, we cannot retain the data types.

I’ve brought this question to one of the devs here to help with a solution, and he was kind enough to write a function that will split and convert your parameters into a multi data type array (only for [string] and [int]). Feel free to import this step template, and pay special attention to the SmartSplitter function. :slight_smile:

{
  "Id": "ActionTemplates-441",
  "Name": "EnumerateCollection",
  "Description": null,
  "ActionType": "Octopus.Script",
  "Version": 4,
  "CommunityActionTemplateId": null,
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptBody": "function SmartSplitter ($parameterFromOctopus){\r\n    [object[]]$values = $null\r\n    \r\n    foreach($value in $parameterFromOctopus.split(',').trim()){\r\n        \r\n        [int]$int = $null\r\n\r\n        if([int]::TryParse($value,[ref]$int)){\r\n            $values += $int\r\n        }\r\n\r\n        else{\r\n            $values += [string]$value\r\n        }\r\n    }\r\n\r\n    return $values\r\n}\r\n\r\n$parameters = SmartSplitter -parameterFromOctopus $collection\r\n\r\nforeach($parameter in $parameters){    \r\n    $type = $parameter.getType().name\r\n\r\n    \"The parameter [$parameter] is of type [$type]\"\r\n}",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Parameters": [
    {
      "Id": "a3247378-63ac-44d8-8e34-28146fc53c28",
      "Name": "Collection",
      "Label": "Collection",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    }
  ],
  "$Meta": {
    "ExportedAt": "2017-06-06T23:28:51.126Z",
    "OctopusVersion": "3.13.7",
    "Type": "ActionTemplate"
  }
}

Let me know how you go and if you have any further questions at all.

Best regards,

Kenny

Kenny,

Hey, String and Int are the two primary types I’ll probably care about - so sweet!

Thanks to you & the devs - We do love our Octopus & you guys are the best :slight_smile:

R

Hi Rachel,

That’s no problem at all! I’m glad it helped. Thanks for the kind words as well (we love hearing that you love Octopus!). :slight_smile:

Don’t hesitate to reach out if you have any further questions.

Best regards,

Kenny