简体   繁体   English

是否可以在Azure DevOps“变量组”中定义数组变量

[英]Can an array variable defined in Azure DevOps “variable group”

I want to define some variables in Azure devops "variable group" which will be used in Powershell, but when the variable type is string it works, but some is array or object went wrong. 我想在Powershell中使用Azure devops“变量组”中定义一些变量,但是当变量类型是字符串时,它可以工作,但是某些变量是数组或对象出错。 mine look like below. 我的样子如下。 left is the name ,right is the value 左边是名称 ,右边是

vmAlertedArray_backup => @("wbubuntu","wbubuntu2")

1.when in azure devops script I use, it went wrong 1.在我使用的Azure devops脚本中,出现错误

$vmAlertedArray_backup = $env:vmAlertedArray_backup foreach($c in $vmAlertedArray_backup){ Write-Host "$c" }

2.below in powershell in local works 2.在本地工作中处于弱势

$vmAlertedArray_backup = @("wbubuntu","wbubuntu2") foreach($c in $vmAlertedArray_backup){ Write-Host "$c" }

Can any one show some experience about this? 有人可以显示一些经验吗? thanks 谢谢

It is suggested to only pass variables as string. 建议仅将变量作为字符串传递。 If you want to pass an object to other tasks, you can use "ConvertTo-Json -Compress" to convert it to a json string. 如果要将对象传递给其他任务,则可以使用“ ConvertTo-Json -Compress”将其转换为json字符串。

$objectString = $object | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=objectString;]$objectString"

And in the next PS task, you can pass it as environment variable. 在下一个PS任务中,您可以将其作为环境变量传递。 But, please enclose the variables in single quotes. 但是,请将变量用单引号引起来。

在此处输入图片说明

And then you can use "ConvertFrom-Json" to convert the string to an object. 然后,您可以使用“ ConvertFrom-Json”将字符串转换为对象。

$getFromEnv = $env:objectString | ConvertFrom-Json
foreach( $obj in $getFromEnv){
    Write-Host ("displayName:{0} Id:{1}" -f $obj.displayName, $obj.Id)
} 

I just pass the variable in as a string and then split it to create an array in PowerShell 我只是将变量作为字符串传递,然后将其拆分以在PowerShell中创建数组

Variable = Prod1,Prod2,Prod3 变量= Prod1,Prod2,Prod3

$array = $variable.Split(',)

If need be you can add a Trim to the end in case there are spaces 如果需要,可以在末尾添加一个Trim

Variable = Prod1,Prod2 ,Prod3 变量= Prod1,Prod2,Prod3

$array = $workspaces.Split(',').trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM