Here's two possible syntaxes. I'm assuming you're defining dbStages as a parameter and doing foreach loop in the pipeline....
As Environment Variables
All pipeline variables are available at runtime as environment variables, so it's possible to access them in several different ways programmatically. Here I'm passing Core/Test/Foo as an env value and then accessing it as an environment variable.
parameters:
- name: dbStages
type: object
default:
- Core
- Test
- Foo
variables:
- group: myVariableGroup
steps:
- ${{ each dbPrefix in parameters.dbStages }}:
- task:PowerShell@2
inputs:
targetType: inline
script: |
$prefix = $env:prefix
$varUserName = $prefix + "UserName"
# access through $env
$UserName = $env:$varUserName
# access through Env: provider
$UserName = Get-Item Env:$varUserName
# access through .NET
$UserName = [System.Environment]::GetEnvironmentVariable($varUserName)
env:
prefix: ${{ dbPrefix }}
You could go one step further, and de-reference CoreUserName to UserName by using the available Logging Commands. Subsequent tasks would be able to use the $(UserName) variable...
$UserName = $env:$( $prefix + "UserName" )
Write-Host "##vso[task.setvariable variable=UserName]$UserName"
Through Macro Syntax
The second option is to access the variables through the $() macro syntax. It's important to realize that parameters are resolved when the pipeline is compiled and variables are evaluated at runtime.
We can leverage compile time parameters to construct the macro syntax with the variable name you want to evaluate at runtime:
- ${{ each dbPrefix in parameters.dbStages }}:
- task: PowerShell@2
inputs:
targetType: inline
script: |
# resolves into "$(CoreUserName)" at compile time
# by is evaluated as "<value-of-CoreUserName>" at runtime
$userName = "$(${{ dbPrefix }}UserName)"