3
votes

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?

2

2 Answers

3
votes

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)!"

3
votes

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"