Cristobal BL
Cristobal BL

Reputation: 135

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

Upvotes: 1

Views: 8573

Answers (2)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13984

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

Upvotes: 1

Matt
Matt

Reputation: 4065

Using the set variable command will make the variable available for all the task steps that follow. It will not be available within the scope of the same task. If you break your task into two steps, one set then one test display, I'd expect you would see the setting is probably going to be as-expected for your postman step.

From the documentation:

To set a variable from a script, use the task.setvariable logging command. This doesn't update the environment variables, but it does make the new variable available to downstream steps within the same job.

Upvotes: 4

Related Questions