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

Esther Fan - MSFT
  • 8,276
  • 4
  • 27
  • 25
cilerler
  • 9,010
  • 10
  • 56
  • 91
  • If you want to change an environment variable value system-wide, [you can set it in registry or use setx.exe](http://stackoverflow.com/questions/573817/where-are-environment-variables-stored-in-registry). – Alexander Obersht Oct 09 '15 at 05:53
  • thx but this is on VSO build machine where we can't access/change registry – cilerler Oct 11 '15 at 11:35
  • See the following blog on MSDN which explains the difference between persisted and not persisted ways to set variables: https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/ – shivesh suman Apr 06 '17 at 18:29

4 Answers4

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

cilerler
  • 9,010
  • 10
  • 56
  • 91
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/

shivesh suman
  • 1,511
  • 18
  • 26
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"
Joe B
  • 692
  • 8
  • 18
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:

Community
  • 1
  • 1
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • 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 Oct 08 '15 at 22:21
  • 1
    @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. – beatcracker Oct 08 '15 at 22:23
  • setting as machine scope thrown a permission error. setting as user scope didn't even change the value in the process. – cilerler Oct 11 '15 at 11:38
  • 2
    @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](http://blogs.msdn.com/b/oldnewthing/archive/2015/09/15/10641604.aspx) for a more in-depth explanation. – beatcracker Oct 11 '15 at 13:51