Im working on deploying arm templates via VSTS pipeline. I have a powershell script which gets a subnet id and I need to pass this subnet id value into another task in VSTS. How can i pass variables from a powershell script to vsts and then pass this to azure resource group deployment task as a parmaeter?
-
1The logging command (as Rodrigo mentioned) can achieve it. After setting the the variable value from a PowerShell task, the changed value can be used in the following tasks if the build. – Marina Liu Mar 14 '18 at 03:01
2 Answers
You can set a new variable in your powershell and then pass it to VSTS, so that it can be used in one of the next tasks, like this.
Write-Host "##vso[task.setvariable variable=sauce]crushed tomatoes"
Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
This will create a new environment variable called 'sauce' (first example) with value 'crushed tomatoes'.
On your next task you can read it using $(sauce) in your task configuration screen or $env:sauce in a powershell script.
See https://learn.microsoft.com/en-us/vsts/build-release/concepts/definitions/release/variables?tabs=batch for more info about how to use it.

- 2,118
- 1
- 17
- 28
-
Here are the docs: https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md – Ohad Schneider Aug 21 '18 at 21:39
The suggestion by @Rodrigo Werlang would work, but the set variable would be scoped to the job the task setting it is in. As of today, this is a known limitation and is documented here.
To define or modify a variable from a script, use the task.setvariable logging command. Note that the updated variable value is scoped to the job being executed, and does not flow across jobs or stages. Variable names are transformed to upper-case, and the characters "." and " " are replaced by "_".