问题
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:
You do not need to set the reference name.
There is a return before the write variable command. So, the write variable command will not be executed.
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