3

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?

3
  • Add a answer and it works for me, please have a check. Commented Jul 11, 2019 at 2:13
  • i think you need to change it to Write-Host $env:azureADGroups Commented Jul 11, 2019 at 7:14
  • I updated my answer for using it in next PS task. Commented Jul 12, 2019 at 2:03

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Jack, thanks for your help so far so good. If I want to pass in the azureADGroupsFromString to the next PS task, should I pass in as string or json object type? I got an error
@wonderfulworldwithcharity You need t o pass it as a string. Because the environment variable can only be a single-line string.
@wonderfulworldwithcharity And it is easy to utilize the string, just use ConvertFrom-Json, you will get a PSObject.
when I use inline script it works fine but I'm actually passing it to a script, it seems like it's not taking it as a string or something, I've uploaded more screenshots
@wonderfulworldwithcharity Enclose variables in single quotes ('), this may help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.