Darqer
Darqer

Reputation: 2887

TFS Build and Powershell: how to acces predefined build variables

I have issues with accessing build variables from powershell script for example I want to access Agent.BuildDirectory. I tried:

$Build.SourcesDirectory
$(Build.SourcesDirectory)

none works.

I know that I can use $Env:TF_BUILD_SOURCESDIRECTORY but not all variables are available this way.

Do you have any suggestions ?

Upvotes: 1

Views: 2658

Answers (2)

Etienne
Etienne

Reputation: 1100

So this is TFS 2017 Update 3. You should be able to do what you are trying to do. Where are you trying to use the variables, as parameter in the build or actually inside a PowerShell script? Those variable can be used as parameters and will be substituted at run time by the agent and passed to scripts, but if you are trying to access them inside a script like $(...) it will not work. You do need to use $env:VARIABLE. All variables in the variables section get converted to environment variables with their name at runtime. So for example if you are running an inline PowerShell like the image bellow, you can use $(..) inline script

or if you want to pass parameters to the a PowerShell script you can also.

passing parameters to script

But from inside a script you cannot.

enter image description here

Upvotes: 3

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31023

You should use $(Agent.BuildDirectory) not $(Agent.SourcesDirectory). Check Agent variables from the link below:

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#agent-variables

Update: Adding a screenshot:

enter image description here

Upvotes: 1

Related Questions