2
votes

enter image description here enter image description here enter image description here

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.

enter image description 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
Add a answer and it works for me, please have a check.Jack Jia
i think you need to change it to Write-Host $env:azureADGroups4c74356b41
I updated my answer for using it in next PS task.Jack Jia

1 Answers

10
votes

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.

enter image description here

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:

enter image description here

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.