如何将CSV中的数值传递到az cli部署作为参数

huangapple go评论53阅读模式
英文:

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

问题

我不太熟悉PowerShell,这个简单问题一直困扰着我。希望有人能指导我正确的方向。

  • 我有一个包含IP范围值的CSV文件
  • 我希望将这些IP值作为参数传递给一个Bicep模板
  • 参数的类型是数组,参见下面的代码片段

CSV文件:

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

我希望将这些IP值传递给Azure Bicep模板的以下参数:

param ipArray array

CLI命令如下:

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

我无法正确填充$ipRange。我已经测试了以下内容并知道它可以工作:

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

所以我需要找出如何根据上述语法构建我的PowerShell变量

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

无法将字符串解析为JSON:
10.0.0.1 10.0.0.52
错误详细信息:额外的数据:第1行第6列(字符5)

任何对正确方向的指导将不胜感激

谢谢!

英文:

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!

答案1

得分: 0

这段代码将根据您的要求转换IP范围:

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

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

huangapple
  • 本文由 发表于 2023年2月14日 22:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定