2
votes

Currently I am working on a big legacy project where each c# project file has a copy job (with a custom tool written in C#) in the post build event.

The post build event looks like

"$(MyEnvVar)\_Build\_Binaries\CopyTool" "$(TargetDir)$(TargetName)" "$(MyEnvVar)\_Binaries"  

As you can see, the path for the copy job is based on the value of an environment variable.

I now want to build another solution which references some of the projects mentioned above.

The build fails because the environment variable is not set for the build.

The build step order in the Build vNext definition:

SetEnv powershell script

[Environment]::GetEnvironmentVariable("MyEnvVar","Process")
Write-Host 'build sources dir'
Write-Host $Env:BUILD_SOURCESDIRECTORY
[Environment]::SetEnvironmentVariable("MyEnvVar", $Env:BUILD_SOURCESDIRECTORY, "Process")
  • Visual Studio Build step
  • Visual Studio Test
  • ... (index, publish, copy, publish artifacts)

But the value of "MyEnvVar" is not accessible in the post build event which is executed by the VS Solution Build step.

How can I achieve that the VS Build step has the value previously set in the powershell script on TFS onPremise (not VSO)?

2

2 Answers

3
votes

Found a solution.

Created a powershell script

Write-Host “##vso[task.setvariable variable=MY_BuildVariable]$Env:BUILD_SOURCESDIRECTORY”

and executed it as the first build step.

Because the build variable has the same name as the environment variable, the value of the environment variable is overwritten with the value of the powershell script.

See Environment variables which states
Note: If you have defined the a variable of the same name (for example PATH) on the variables tab, then your value overrides the environment variable when you use it as shown above.

0
votes

In Powershell, you can get or set any environmental variables by simply using the below ways:

#for a particular environmental variable
Get-Childitem env:computername
# for getting all the environmental variables
get-childitem -path env:*

#for Setting any value in any environmental variable
$env:Variable1 ="value1"

# For changing the vale of environmental variables
Set-Item -path env:Variable1 -value "Value2"