1
votes

In an Azure DevOps release pipeline, I have an Agent job. Within that Agent job, I have an Azure Powershell task. I do not see a way to specify environment variables for the Azure Powershell script that the Azure Powershell task runs.

You can do it in a build pipeline by simply specifying "env" for the Azure PowerShell task:

- task: AzurePowerShell@5
  inputs:
    azureSubscription: '...'
    ScriptType: 'FilePath'
    ScriptPath: '...'
    ScriptArguments: '...'
    FailOnStandardError: true
    azurePowerShellVersion: 'LatestVersion'
  env:
    USERNAME: $(username)
    PASSWORD: $(password)

But I don't see any way of specifying "env" in a release pipeline:

Screenshot showing no way to add environment variables

How can this be done?

2
Not get your response for several days, would you please share your latest information about this issue? If you have any concern, feel free to share it here. - Hugh Lin
@HughLin-MSFT Sorry for the delayed response. I just responded to your answer below. - user1172282

2 Answers

1
votes

When a pipeline executes, Azure will place all pipeline variables into environment variables, so any tools, scripts, tasks, or processes you run as part of the build can access parameters through the environment.

Defining variable in the DevOps pipeline GUI:

enter image description here

To get the secret variable in azure powershell task, just use the syntax of $(varibaleName) to work.

-1
votes

What I have done with variables in a powershell script in a release pipeline is use the variable Tab where you can either add the variable pair in the pipeline variables or create a variable group in the library section and then link it.

So if you create a variable pair for say compname computer1 then in the actual powershell script reference the variable as get-azvm -name $env:compname

This will then pull the variable you have added