kwdevops
kwdevops

Reputation: 23

Convert array parameter values to comma separated string

I am deploying Vms to Azure with an ARM template. In my parameters file I have:

"nodePrivateIps": {
  "value": [
    { "IpAddress": "10.0.10.1" },
    { "IpAddress": "10.0.10.2" },
    { "IpAddress": "10.0.10.3" }
  ]
},

I am able to use this array in the template with:

"privateIPAddress": "[parameters('nodePrivateIps')[copyIndex()].IpAddress]",

Problem is now I need to pass that same array to my powershell script by using a CustomScriptExtension but it doesn't seem to like the array parameter.

"properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.4",
        "settings": {
          "fileUris": [
            "[concat(parameters('_artifactsLocation'),'/SolrCloudSetup.ps1', parameters('_artifactsLocationSasToken'))]"
          ],
          "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\SolrCloudSetup.ps1 ', parameters('nodePrivateIps'))]"
        }
      }

This is my powershell script parameter that's currently expecting an array. I can easily change that to comma separated string if I get the ARM to cooperate.

# SolrCloudSetup.ps1 -----------------------
param (
   [array] [Parameter(Mandatory=$true)] $solrNodeIps
)

Is there a way to convert this array parameter into a comma separated string so that I can pass it through the CustomScriptExtension to my powershell script?

Upvotes: 2

Views: 3971

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

There is a string() function which is your best bet. You could combine that with replace function to achieve what you need:

"[replace(replace(string(parameters('testArray')), '[', ''), ']', '')]"

In your case its going to be harder, since your array is not just a bunch of strings, so I would use a loop to "convert" your array to a bunch of strings. or better convert your array to this:

[
"10.0.10.1",
"10.0.10.2",
"10.0.10.3"
]

and you can use it like so:

"[parameters('nodePrivateIps')[copyIndex()]]"

Upvotes: 5

Related Questions