问题
Is it possible to use variable inside a variable in Powershell script with Azure Pipelines variables?
SCENARIO
Two variables are set in Azure Pipelines Variable group -
DeploymentCredentials
a.
DeployUATApiPassword = "123456"
b.
DeployPRODApiPassword = "789654"
Another variable is set in the Variables section of the pipeline as
DeploymentEnvironment
At runtime, the value of the
DeploymentEnvironment
variable is set as eitherUAT
orPROD
Based on the enviornment, I want to fetch the password which is stored as a variable, defined in the variables group. At runtime, it should work something like...
# setting the value of the variable to UAT Write-Host "##vso[task.setvariable variable=DeploymentEnvironment]UAT"
Now
DevelopmentEnvironment
variable has the valueUAT
, In another step, I want to fetch the Password for the UAT deployment environment, which is only known at runtime.# I want to get the Password # The following works! but not useful for me, UAT is hard-coded!! $deployPwd = $(DeployUATApiPassword) # works! but not useful for me, UAT is hard-coded!! # I want to get the Password # the following doesn't work, I only know the environment at runtime # Replaces the inner variable to `DeployUATApiPassword` # Raises an error, DeployUATApiPassword : The term 'DeployUATApiPassword' is not recognized as the name of a cmdlet... $deployPwd = $(Deploy$(DeploymentEnvironment)ApiPassword) # All the following didn't work either, but no error... Write-Host $('Deploy$(DeploymentEnvironment)ApiPassword') # Output: DeployUATApiPassword Write-Host $($("Deploy$(DeploymentEnvironment)ApiPassword")) # Output: DeployUATApiPassword Write-Host { 'Deploy$(DeploymentEnvironment)ApiPassword' } # Output: 'DeployUATApiPassword' Write-Host { Deploy$(DeploymentEnvironment)ApiPassword } # Output: DeployUATApiPassword Write-Host { $(Deploy$(DeploymentEnvironment)ApiPassword) } # Output: $(DeployUATApiPassword)
Similarly, I tried to fetch the pwd in several ways, nothing worked.
For instance, one of the ways was to create another variable DeployApiPassword
and I did set the value of the variable to Deploy$(DeploymentEnvironment)ApiPassword
. No luck in this either...
$deployPwd = $(DeployApiPassword) # Output: DeployUATApiPassword
DESIRED OUTPUT
I want to fetch the password from one of the variables which is set in a variables group. The password variable name contains the deployment environment. Deployment environment is only set at runtime.
# I know something is wrong with the following, but that's how I want to run.
$deployPwd = $(Deploy$(DeploymentEnvironment)ApiPassword)
Write-Host $deployPwd # output: *** (of course, the pwd is hidden, but at least it fetches)
回答1:
Variables set in Azure Pipelines Variable group would be used as environment variables in PowerShell task. So, basically, you need to get the value as :
$environment = $env:DeploymentEnvironment
$deployPwd = $env:DeployUATApiPassword
At the same time, as your want to dynamically get the password based on the environment, you may try the following:
$environment= $env:DeploymentEnvironment
Write-Host "Deployment Environment is " $environment
$realName = $("Deploy${environment}ApiPassword")
$pwd = [Environment]::GetEnvironmentVariable($realName)
#I used UAT password, check it here
Write-Host $pwd.Equals("123456")
Output:
回答2:
I noticed that the value returned by 'Deploy$(DeploymentEnvironment)ApiPassword'
is a string type, however $()
accepts a variable not a string.
In the secret variables official document, it says the secret cannot be referenced directly like normal variable. You have to manually explicitly map them in using the Environment section.
You will need to use if statement
. And in the Environment Variables sections
Map the secret variables to variables UATpassword
and PRODpassword
and Then refer to them in the scripts using $env:UATpassword
From below log, we can see the password is retrieved in the script.
来源:https://stackoverflow.com/questions/59669787/how-to-unpack-or-fetch-a-value-of-a-nested-variable-stored-in-azure-devops-build