3

I have a pipeline variable called TestVariable.

I can easily access this variable from a PS script like so:

write-host $(TestVariable)

But if the name of that variable was dynamic, is there any way I can access the variable value from PS?

For example, the name of the variable would go into a string variable. I've tried these combinations as experiments...they just return the variable name, not the value (not surprisingly):

$varname="TestVariable"

write-host $($varname)
write-host $("$varname")
write-host $"($varname)"
write-host $("($varname)")

I think the answer is no, but I want to be sure. Any help much appreciated!

Edit - note

Both answers answer the question but don't solve my problem. After trying the solutions I realized I missed an additional complication which the answers don't help with unfortunately. Am noting here in case someone tries to do something similar.

The extra complication is, the value of the variable is set during the release (I'm trying to access ARM template output variables).

I thought I may be able to hit the API and get the 'live' variable value but unfortunately the release data does not exist (from the API) until the release completes.

So when I call this during a release:

https://vsrm.dev.azure.com/{company}/{project}/_apis/release/releases/$($releaseId)?api-version=5.0

I get "Release with ID 38 does not exist".

wallismark
  • 1,766
  • 2
  • 21
  • 35

4 Answers4

3

Dynamic variable name in VSTS (Azure DevOps) pipeline

Agree with Krzysztof Madej. There is no out of box way to achieve this.

That because the nested variables (like $($varname) are not yet supported in the build pipelines.

To resolve this issue, you could use the Definitions - Get to get the value of Dynamic variable:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1

Below is my test powershell script:

$varname="TestVariable"

$url = "https://dev.azure.com/YourOrganizationName/YourtProjectName/_apis/build/definitions/<definitionsId>?api-version=5.0"

Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}


$VFDV= $pipeline.variables.$varname.value


Write-Host This is Value For Dynamic Variable: $VFDV

enter image description here

The output:

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
3

Late to the party, but figure I'd share.

As mentioned in the Defined Variables doc, pipeline variables are accessible through the environment variables. While $(varname) gets processed before the task starts, $env:varname can be invoked mid-run. So you can cheat by using:

Write-Host ('$env:'+"$(varname)" | Invoke-Expression)

The task will resolve $(varname) into its value before the task begins. So the script reads as

Write-Host ("$env:TestVariable" | Invoke-Expression)

And it'll spit out the same as calling $(TestVariable).

Though you do need to respect the rules, such as " " and "." -> "_".

  • There is a stray quote character at the end of that first line. It should be: Write-Host ('$env:'+"$(varname)" | Invoke-Expression) – Dustin C Dec 23 '20 at 18:29
  • unfortunately not all variables are available as environment variables – Karpik May 10 '23 at 16:27
2

This is not possible directly in YAML but you can use for instance az cli. With this you can set programatically a variable name a get value of it

$variableName = "some"

az pipelines variable list --org "https://dev.azure.com/thecodemanual" --project "DevOps Manual" --pipeline-name "DevOps Manual-CI" --query ($variableName + '.value')

$variableName = "test"

az pipelines variable list --org "https://dev.azure.com/thecodemanual" --project "DevOps Manual" --pipeline-name "DevOps Manual-CI" --query ($variableName + '.value')

Now you can use this code in a powershell task to fetch value of variable.

Here you have info how to install extension.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
-1

I know its too late but I can tell you a perfect solution. as we know all the pipeline variables are available as environment variables so we can access the values as below

var=$(TestVariable)
upper= ${var^^} //convert the variable to uppercase as env variables are upper
echo ${!upper}

Please note the above solution is tested and works in bash only. I haven't written for PS