Search code examples
powershellazure-bicep

How to pass values from csv into az cli deployment as parameters


I don't have much experience with PowerShell and this simple issue has been driving me up the wall. I'm hoping someone can point me in the right direction.

  • I have a CSV-file with IP-range values
  • I wish to pass these IP values as a parameter to a Bicep template
  • The parameter is of type array, see code snippets below

CSV-file:

IP,Comment
10.0.0.1, Comment blabla
10.0.0.52, Comment more blabla

I wish to pass the IP-values into a Azure Bicep template with the following parameter:

param ipArray array

The cli command is as follows:

az deployment group validate -g test-rg -f .\main.bicep -p ipArray=$ipRange

I am unable to populate $ipRange properly. I have tested the following and know it works:

az deployment group validate -g test-rg -f .\main.bicep -p ipArray="['10.0.0.1','10.0.0.52']"

So I need to figure out how to build my Powershell variable according to above syntax

$ipRange = ((Get-Content .\ip_list.csv) | ConvertFrom-Csv).IP

Failed to parse string as JSON:
10.0.0.1 10.0.0.52
Error detail: Extra data: line 1 column 6 (char 5)

Any nudge in the right direction will be greatly appreciated

Thanks!


Solution

  • This code will convert the ip range as you asked for:

    $ipRange = ((Get-Content C:\Temp\ip.csv) | ConvertFrom-Csv).IP | ConvertTo-Json
    $ipRange = $ipRange.ToString() -replace '"',"'"
    $ipRange