10

I have a vsts build definition in which I try to set the PATH environment variable using PowerShell (and before I tried cmd) task, so that in a later vsTest task, the tests could run an exe from that path, however setting the PATH using the ps\cmd tasks doesn’t seem to work, I tried a few options such as:

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";" + $newPath, [EnvironmentVariableTarget]::User)

setx path " %newPath;%PATH%"

Any suggestions?

uril
  • 173
  • 1
  • 8
  • 1
    Environment variables are usually inherited by sub-processes, but processes that are invoked in succession won't be able to pick up the values that way. You may be able set a file that can be queried in your later test task to get the PATH you need. – Bob Dalgleish Mar 14 '18 at 23:19

2 Answers2

13

Set the process environment variable by calling logging command through PowerShell task:

For example:

Write-Host "##vso[task.setvariable variable=PATH;]${env:PATH};$newPath";
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Yes, I ended up doing just that – uril Mar 15 '18 at 12:11
  • Btw, this is not working properly if you're injecting a secret variable that contains a dollar sign in its value. The dollar signs are removed from the final value. I came across this specific problem when injecting a secret connection string containing a dollar sign. – baumgarb Jul 05 '18 at 10:48
  • Make sure you _don't_ add `isOutput=true` as parameter to `task.setvariable` - it didn't work for me then. – White hawk Dec 01 '18 at 11:34
  • How should one use this in Kudu for an azure function? I get: $newPath = "D:\home\python364x86" PS D:\home> Write-Host "##vso[task.setvariable variable=PATH;]${env:PATH};$newPath"; Write-Host "##vso[task.setvariable variable=PATH;]${env:PATH};$newPath"; Write-Host : The Win32 internal error "The handle is invalid" 0x6 occurred – Norbert Huurnink Apr 02 '19 at 17:49
3

If you need to do this in a Linux pipeline you can do the following:

- script: echo "##vso[task.setvariable variable=PATH]${PATH}:<your new path here>"