17

I have set a build variable which I would like to consume in my inline powershell script.

I tried $($myvariable) but this comes out blank.

0

4 Answers 4

15

Pass build variables to powerhshell inline script in Azure DevOps

To get the build variables with inline power shell script, you can try to use following syntax $env:variable:

$env:myvariable

enter image description here

The build variable:

enter image description here

The build result:

enter image description here

Note:At this moment, the value of nested variables (like $(myvariable$(Build.SourceBranchName))) on build variables table are not yet supported in the build pipelines.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

How would you get the secret variables passed into the inline script like this? I have used ConvertTo-SecureString "$($password)" -AsPlainText -Force, but that was the $ password was passed into an external script. It didn't work for inline scripts.
5

here's what's working for me:

$(variableName)

for example built-in variable called Build.BuildNumber can be accessed like so:

$(Build.BuildNumber)

full example with format function:

- task: AzurePowerShell@3
  displayName: UpdatePrereq
  inputs:
    azureSubscription: ${{ parameters.azureSubscription }}
    ScriptType: InlineScript
    Inline: |
        ${{ format('. $(Build.Repository.LocalPath)\scripts\_helpers.ps1
        Update-DeploymentPrereq -resourceGroup {1} -location {3}
        Update-Prereq -pathSuffix {0} -pathBase $(Build.Repository.LocalPath) -resourceGroup {1} -buildId $(Build.BuildNumber) -paramFile {2}
        Update-DeploymentConcurrency -resourceGroup {1} -buildId $(Build.BuildNumber)',
            parameters.buildDir, parameters.resourceGroupName, parameters.paramFile, parameters.location ) }}
    azurePowerShellVersion: LatestVersion

Comments

1

If you need to use nested variables you can use something like this:

$name = "CONNECTIONSTRING_$(Agent.MachineName)"
$val = [System.Environment]::GetEnvironmentVariable($name)

And then you get the value of the variable named CONNECTIONSTRING_MACHINENAME

Comments

0

Use $env:variableName

If your variable names contain dots, replace the dots with underscore:

  Write-Host "variable.name= $env:variable_name"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.