Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps - Setting and Using Variables in PowerShell Scripts

Tags:

I have an Azure DevOps build pipeline that has two separate PowerShell scripts. In the first script, I am getting a value from an XML file and setting that value in an environment variable. In my second script, I want to use the value in the environment variable. Unfortunately, I don't see the environment variable getting set. At this time, I have:

Script 1:

$myXml = [xml](Get-Content ./MyXml.xml)   $departmentId = $myXml.Department.Id  Write-Host ##vso[task.setvariable variable=DepartmentId;]$departmentId     Write-Host "Set environment variable to ($env:DepartmentId)"  Get-ChildItem Env:  Write-Host "Department Id ($departmentId)" 

When script 1 runs, I see:

Set environment variable to () [All of the environment variable BUT, I DO NOT SEE ONE NAMED "DepartmentId"] Department Id (1) 

Notice: 1) The $env:DepartmentId value is not printing in the "Set environment variable" statement and 2) The DepartmentId value is NOT listed in the environment variable list. My intention is to use DepartmentId in the second script, which looks like this:

Script 2:

Write-Host "Using Department: $(env:DepartmentId)" 

At this time, the script just shows:

env:DepartmentId : The term 'env:DepartmentId' 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. 

I've seen the other related SO questions and reviewed the docs. However, this simply isn't working. I don't understand what I'm doing wrong. Can someone please show me how to fix this and explain what I'm doing wrong? Thank you!

like image 731
Some User Avatar asked Sep 14 '18 14:09

Some User


People also ask

How do you access pipeline variables in PowerShell?

You can directly use the expression “$env:VAR_NAME” in your PowerShell scripts to access the environment variables that have been mapped on the agent, regardless of the script type is File Path or Inline.

How do you access variables from variable group in Azure DevOps?

To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.

How do you pass variables between tasks in Azure DevOps?

Share variables between Tasks across the Jobs (of the same Stage) We need to use the isOutput=true flag when you desire to use the variable in another Task located in another Job. Navigate to Stage1_Job1_Task1 and add isoutput = true flag to the Logging Command which let's us to access the value outside the Job.


2 Answers

Script 1

Use quotes when setting the environment variable via task.setvariable since # signifies a PowerShell comment. You have commented out the string you intend to output.

Also note that the environment variable may not be available in the script where you set it since the pipeline must first process task.setvariable in the output.

$myXml = [xml](Get-Content ./MyXml.xml)   $departmentId = $myXml.Department.Id  Write-Host "##vso[task.setvariable variable=DepartmentId;]$departmentId" Write-Host "Set environment variable to ($env:DepartmentId)"  Get-ChildItem Env:  Write-Host "Department Id ($departmentId)" 

Script 2

You must still reference variables via $ inside an expression. You're missing $ before env.

Write-Host "Using Department: $($env:DepartmentId)" 
like image 185
Dan Wilson Avatar answered Oct 21 '22 02:10

Dan Wilson


To set environmental variables you need to set it using

$env:departmentId = $myXml.Department.Id 

When using variables within strings you still need the $ sign in front of variables. As in

Write-Host "Using Department: $($env:DepartmentId)" 

The reason environmental variables look different for get-childItem is that you are actually listing a psprovider, not the accessing the variable.

Get-ChildItem Env: 

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-6

like image 40
Jordan Avatar answered Oct 21 '22 01:10

Jordan