Azure DevOps Azure PowerShell任务输出变量 [英] Azure DevOps Azure PowerShell task output variable
问题描述
我正在使用一个Azure PowerShell任务和PowerShell任务创建一个发布管道.在Azure Powershell任务中,我有以下代码
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"
我正在尝试将$ groupInfos存储到此处的azureADGroups变量中.
I am trying to store $groupInfos into azureADGroups variable here.
但是当我在同一任务下的下一步中运行PowerShell任务时,它说无法识别术语"azureADGroup".似乎未设置变量.有人知道我在这里缺少什么吗?/p>
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个问题:
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.
write variable命令只能使用单行字符串.但是,$ groupInfos是一个对象.它不会隐式转换为字符串.您需要使用"ConvertTo-Json -Compress"命令将其转换为字符串.
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 "
从调试日志中,我们可以检查是否成功设置了变量"azureADGroupsFromString".
From the debug log, we can check that variable "azureADGroupsFromString" is successfully set.
更新:
您可以在下一个PS任务中使用以下脚本:
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)
}
输出:
更新:
如果要通过参数将其传递给下一个PS任务,请将该变量括在单引号中.这样,它将被视为字符串.
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.
这篇关于Azure DevOps Azure PowerShell任务输出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!