Azure DevOps Azure PowerShell task output variable

落花浮王杯 提交于 2020-06-16 06:05:28

问题


I'm creating a release pipeline using one Azure PowerShell Task and PowerShell task. In the the Azure Powershell Task, I have the following code

$groupInfos = @()
for ([int]$i = 0; $i -lt $azureADGroupsObj.Count)
{
    $groupInfo = New-Object PSObject
    $groupInfo | Add-Member -MemberType NoteProperty -Name "displayName" -Value $azureADGroupsObj[$i].DisplayName
    $groupInfo | Add-Member -MemberType NoteProperty -Name "Id" -Value 
    $azureADGroupsObj[$i].Id
    $groupInfos += $groupInfo
    $i++
}
return $groupInfos
Write-Host "##vso[task.setvariable variable=azureADGroups;]$groupInfos"

I am trying to store $groupInfos into azureADGroups variable here.

but when I run a PowerShell task in the next step under same job, it says the term "azureADGroup" is not recognized.. seems like the variable wasn't set..does anyone know what am I missing here?


回答1:


I found 3 problems in your script:

  1. You do not need to set the reference name.

  2. There is a return before the write variable command. So, the write variable command will not be executed.

  3. The write variable command can only use single-line string. However, the $groupInfos is an object. It will not be implicitly converted to a string. You need to use "ConvertTo-Json -Compress" command to convert it to a string.

I tested at my pipeline:

$groupInfosString = $groupInfos | ConvertTo-Json -Compress
write-host $groupInfos
write-host $groupInfosString 
Write-Host "##vso[task.setvariable variable=azureADGroups;]$groupInfos"
Write-Host "##vso[task.setvariable variable=azureADGroupsFromString;]$groupInfosString "

From the debug log, we can check that variable "azureADGroupsFromString" is successfully set.

Update:

You can use the following script in next PS task:

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

Output:

Update:

If you want to pass it to next PS task via arguments, please enclose the variable in single quotes. In this way, it will be considered as a string.



来源:https://stackoverflow.com/questions/56980502/azure-devops-azure-powershell-task-output-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!