'How to set Azure DevOps pipeline variable with Powershell

I currently have a variable in my release pipeline and need to set its value through a Powershell script. The purpose is to have its value available to be used for postman collections in next tasks.

I'm trying to do that in this way but not working.

$content = Get-Content -Path .\token.txt

Write-Host "RP token found: $content"

Write-Host "##vso[task.setvariable variable=readingProgressToken;]$content"

Write-Host "Variable value in pipeline: $(readingProgressToken)"

And this is the variable variable



Solution 1:[1]

In the script task (PowerShell, Bash, etc..), if you use the logging command SetVariable to create or update a variable with the specified value, this variable with the new value is only available to the subsequent tasks in the same job.

For the script task which runs the SetVariable command, the new value is not available.

So, if you want to print the new value of the variable, you should print it in another task after the script task.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1