Are pipeline variables available in PowerShell task in Azure DevOps?
Asked Answered
S

3

8

I have this PowerShell script, that reads some environment variables and outputs them:

Write-Host "name: $(tenantName) version: $(versionRelease) url: $(urlApplication)"

Those variables are defined as pipeline variables and also in variable groups in an Azure DevOps release pipeline.

When run inside a PowerShell task defined as Inline, this script works as expected. However, the same exact script won't work when the PowerShell task is configured with a File Path. In this case, the tenantName env variable is not found:

tenantName : The term 'tenantName' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Is there any way to make the environment variables available to a "File" script as they are to an "Inline" one?

Seiler answered 16/6, 2020 at 13:0 Comment(0)
S
8

I managed to fix it, partially thanks to @Cbsch's answer. Indeed, the syntax of my PowerShell script was not correct, but changing $(tenantName) to $tenantName didn't fix the problem, since the $tenantName didn't exist. I had to read it from the environment variables, with the $env:<variableName> syntax.

The fixed script:

Write-Host "name: $env:tenantName version: $env:versionRelease url: $env:urlApplication"
Seiler answered 16/6, 2020 at 14:13 Comment(0)
D
5

I can't answer to the Azure part of it, but your code is not valid for what you are trying. You need to include the $ inside the $() blocks.

As it is now, Powershell is trying to call a function named tenantName, or find an application named tenantName.exe to call.

This will work:

Write-Host "name: $($tenantName) version: $($versionRelease) url: $($urlApplication)"

Or even better:

Write-Host "name: $tenantName version: $versionRelease url: $urlApplication"

The $() syntax is used if you need to put some actual code into the string, like referencing a property, like Write-Host "Hello $($user.name)!"

Disentail answered 16/6, 2020 at 13:6 Comment(0)
I
0

You can't access the variables using $ or $() until you map it. Once mapped, you can access the variables using $env:ENV_NAME.

Step 1) Define your variable and its value under Variables tab inside Pipeline Edit Variables in Release Pipeline

Step 2) Under Environment Variables, map your variable from step 1 with ENV Variable name that will be used inside the Powershell scripts. Environment Variable Mapping in Azure DevOPS

Step 3) Now, access your variable with the name used in Step 2. For example, in my case it will be $env:ENVPWD It will give me the value of the variable configured in Step 1 inside the script during runtime.

Hope that helps!

Inrush answered 19/9 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.