sppc42
sppc42

Reputation: 3292

Azure Devops: Saving output of a command line task to a variable to be used in another step

In Azure Devops, am able to add a commandline task. That commandline produces an output that I want to capture and be able to save that to a variable, if possible, to be used in later steps.

Eg, consider my commandline task just does flat listing of files in a directory dir /b producing the output below

c:\temp\foo.txt
c:\temp\bar.exe

I want to capture the above listed directories and use them in another Azure Devops task.

How can I do that?

Upvotes: 3

Views: 4327

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35194

I want to capture the above listed directories and use them in another Azure Devops task.

You could use the script to get the task output. Then set the output as environment variable.

Here are the Command Line Taks script:

SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`dir /b`) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)

echo "##vso[task.setvariable variable=testvar;]%var1%"
echo "##vso[task.setvariable variable=testvar1;]%var2%"

ENDLOCAL

You could use the variables in the following tasks via $(variable name).

For example:

echo $(testvar)
echo $(testvar1)

Here is the result:

Test Result

Notes: The dir /b output is multi-line value. But the variables doesn't support Multi-line value.

You can split the output into multiple variables or concatenate into a string.

concatenate into a string

echo "##vso[task.setvariable variable=testvar;]%var1%;%var2%

Hope this helps.

Upvotes: 2

Related Questions