John Zachariah
John Zachariah

Reputation: 121

How to get current Build definition name using PowerShell in TFS

We are using TFSdeployer to trigger Build deployment to deploy build.

But stuck with how to get Build definition of the current Build for which we trying and could not succeed.

How to get current Build definition name using below PowerShell script instead of hardcode.

Scenario: If i trigger from Test1 BuildQuality then first code should be working . If i trigger from Test2 BuildQuality then second code should be working. (If-Else code is working perfectly)

Tried using $TFSDeployerBuildDetail.DefinitionName $Build.Name

But that seems to be not working, Any help is appreciated.

$BuildDefinitionName =  "Test1"
$PackagePath = "C:\extra\Package_Files"
$DestPath1 ="C:\extra\Test1"
$DestPath2 ="C:\extra\Test2"
$logFile ="C:\extra\logs"
$utilPath = "C:\util\"


if ($BuildDefinitionName -eq "Test1")
{

Write-Host "Testing 1st Copy"

Add-Content -Path $logFile "Copying to Test1"
& $utilPath\robocopy $PackagePath $DestPath1 /s >> $logFile

}

elseif($BuildDefinitionName -eq "Test2")
{

Write-Host "Testing 2nd Copy"
Add-Content -Path $logFile "Copying to Test2"
& $utilPath\robocopy $PackagePath $DestPath2 /s >> $logFile

}

Upvotes: 1

Views: 2287

Answers (2)

Daniel Mann
Daniel Mann

Reputation: 58980

Since you indicated you're using XAML build, the environment variable would be $env:TF_BUILD_BUILDDEFINITIONNAME.

The other answer is correct for the newer, web-based build system.

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72151

Use the provided environment variable:

$env:BUILD_DEFINITIONNAME

https://learn.microsoft.com/en-us/vsts/build-release/actions/scripts/powershell

Upvotes: 2

Related Questions