powershellazure-devopsazure-pipelines-yaml

Accessing Azure DevOps variable (in variable group) from PowerShell Script FILE


I have a variable smtpPassword=mypassword in an Azure Devops variable group.

This prints it (albeit in *** form):

steps:
- powershell: Write-Host $(smtpPassword)

This doesn't. Its just blank:

steps:
- task: PowerShell@2
  inputs:
      filePath: ./script.ps1
# script.ps1
Write-Host $env:SMTPPASSWORD

WHY?! I have tried everything!!! Please help. It works if you set the variable in the yaml file itself just not from a variable group.


Solution

  • You'll need to map secret variable as environment variables to reference them in YAML pipelines.

    steps:
    - task: PowerShell@2
      env:
        SMTPPASSWORD: $(smtpPassword)
      inputs:
          filePath: ./script.ps1
    

    This will print it as ***, see details here