且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Azure DevOps Azure PowerShell任务输出变量

更新时间:2023-11-04 11:15:22

我在您的脚本中发现了3个问题:

I found 3 problems in your script:

  1. 您不需要设置参考名称.

  1. 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.