7

Is there a way to persist changes in environment value between tasks in Visual Studio Team Services? I'm using Powershell to change it but it only changes it in the task not the whole process.

script 1

Write-Verbose "Before: $Env:SuperVersion"
$Env:SuperVersion = $NewVersion
Write-Verbose "After: $Env:SuperVersion"

script 2

Write-Verbose "Final: $Env:SuperVersion"

I see the change at After but Final is always getting the original value

3

4 Answers 4

17

Based on this issue following line will do the trick.

Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")

You may find more commands like that in here

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

1 Comment

you don't need the brackets around the string in powershell ;)
1

Correct answer has already been posted for this question below, however I think that the discussion presented at the following blog specifically targets the two different ways of setting build variables: one in which the variable will be available only within the specific task in which it is set and another using which you can set a build variable in one task and then access it in another:

https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/

Comments

1

I find that after using Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")
that within the same task, the value has not changed, but in later tasks that value has changed.

This is on TFS 2018 using inline powershell.

FIRST TASK

$ENV:SuperVersion = "2.0"
Write-Host ("##vso[task.setvariable variable=SuperVersion;]"3.2"")
#  Output will be "2.0"
Write-Output $ENV:SuperVersion     
$ENV:SuperVersion = "5.5"
#  Output will be "5.5" but only within the scope of this task.
Write-Output $ENV:SuperVersion 

NEXT TASK

Write-Output $ENV:SuperVersion     
# Output is "3.2"

Comments

0

Environment variables created with $env: are Process variables, so they're lost when the process exits and you can't access them from another process (PowerShell instance).

You need to create User or Machine environment variable:

[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'User')

[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'Machine')

I'm not sure though, that it will work in VS Team Services, you'd have to test it.

Reference:

4 Comments

Thank you. Unfortunately variable is already exist on environment. And I'm not sure about its scope. That is the reason that I mention "Visual Studio Online Build" process
@cilerler If it exists, you're not setting it "globally" anyway with $env:, only per-process. It wouldn't work even on standard Windows environment.
setting as machine scope thrown a permission error. setting as user scope didn't even change the value in the process.
@cilerler "setting as user scope didn't even change the value in the process." That's correct, setting User/Machine env. variable wouldn't update per-process variables for already running processes. But it should make it available next time the new process starts. See this article for a more in-depth explanation.

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.