6

I have an Azure Pipeline setup with MyVariable variable defined:

MyVariable

How do I write Azure PowerShell Inline Script to read the variable, and set it to a value after some processing?

You can write your azure powershell scripts inline here.

Super Jade
  • 5,609
  • 7
  • 39
  • 61
CSon
  • 61
  • 1
  • 1
  • 2

2 Answers2

8

Reading:

Variables are exposed as environment-variables, to read the variable "TestVar" you can do this:

$myScriptVariable = $env:TESTVAR

Note that "." will be replaced with "_" and all is uppercase.

Setting or Updating:

To set or update a variable you'll have to write following 'command' to the host with "write-host":

Write-Host "##vso[task.setvariable variable=testvar;]testvalue"

There are more logging-commands for different actions, i'll just leave the link to the documentation here -> https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md

D.J.
  • 3,644
  • 2
  • 15
  • 22
  • Be advised that uppercase is important. I went down a rabbit hole of trying to get this to work while using lowercase characters and my pipeline kept failing. It was because you must do all caps and replace periods with underscores as this answer suggests. – Brandon Avant Jan 06 '21 at 20:31
2

build variables are exposed as environment variables inside build steps, so you can just reference it using normal powershell syntax:

$env:MyVariable
4c74356b41
  • 69,186
  • 6
  • 100
  • 141