locked
Create TFS Build Definition dynamically using Powershell RRS feed

  • Question

  • Hi,

    I am new to powershell. I need to write a script to create the empty build definition. It will be like a template and the developers can go a head and make the necessary changes to make it work. i am using TFS 2010

    For Example:

    TFS CollectionUrl: http: / / tfs.myserver.com: 8080 /tfs/my_collection
    Project Name: MyTeamProject
    buildDefinition Name: MyTeamProject_Dev_Build
    Solution Path:  “$/MyTeamProject/SolutionDir”;
    Drop Location: \ \ Shared \ server \ Drops

    Everything will be hard coded in the script, i'll just pass the Project name when i run the scripts.

    Can someone help me with the script. any help will be greatly appreciated.

    Thanks you

    Tuesday, March 5, 2013 9:26 PM

Answers

  • Hi,

    Thanks for your patiences!

    After consult someone familiar with it, I got the answer of it:

    Recycling from a bigger scripts: please test and double check.

    param(
                 [Parameter(Mandatory=$true)]
                 [string] $buildName,
            [string] $serverName="http://tfs:80/",
            [string] $teamProject="MyProject"
    )

    # VS 2010
    # $tfsClientVersion = ", Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    # VS 2008
    $tfsClientVersion = ", Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client"+$tfsClientVersion)
    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.Build.Client"+$tfsClientVersion)
    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.VersionControl.Client"+$tfsClientVersion)

    $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
    $versionControl = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    $buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

    $def = $buildserver.CreateBuildDefinition($teamProject)
    $def.Name = "$buildName"
    $def.Description = "Created by Powershell script "
    $agentSpec = $buildServer.CreateBuildAgentSpec($teamProject)
    $agentSpec.Name = "build"
    $agent = $buildServer.QueryBuildAgents($agentSpec).Agents[0]
    $def.DefaultBuildAgent = $agent
    $def.DefaultDropLocation = "\\build\PrivateDrops"
    $def.RetentionPolicies.Item('Failed').NumberToKeep = 5
    $def.RetentionPolicies.Item('Stopped').NumberToKeep = 0 #None
    $def.RetentionPolicies.Item('PartiallySucceeded').NumberToKeep = 5
    $def.RetentionPolicies.Item('Succeeded').NumberToKeep = 5
    $def.Save()

    Best Regards, 


    Cathy Kong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 8, 2013 1:02 AM
    Moderator

All replies

  • Hi,

    Thanks for your post!

    I am currently looking into this issue and will give you an update as soon as possible.

    Thank you for your understanding and support.

    Best Regards,


    Cathy Kong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 7, 2013 2:20 AM
    Moderator
  • Hi,

    Thanks for your patiences!

    After consult someone familiar with it, I got the answer of it:

    Recycling from a bigger scripts: please test and double check.

    param(
                 [Parameter(Mandatory=$true)]
                 [string] $buildName,
            [string] $serverName="http://tfs:80/",
            [string] $teamProject="MyProject"
    )

    # VS 2010
    # $tfsClientVersion = ", Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    # VS 2008
    $tfsClientVersion = ", Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client"+$tfsClientVersion)
    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.Build.Client"+$tfsClientVersion)
    [void][System.Reflection.Assembly]::Load("Microsoft.TeamFoundation.VersionControl.Client"+$tfsClientVersion)

    $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
    $versionControl = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    $buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

    $def = $buildserver.CreateBuildDefinition($teamProject)
    $def.Name = "$buildName"
    $def.Description = "Created by Powershell script "
    $agentSpec = $buildServer.CreateBuildAgentSpec($teamProject)
    $agentSpec.Name = "build"
    $agent = $buildServer.QueryBuildAgents($agentSpec).Agents[0]
    $def.DefaultBuildAgent = $agent
    $def.DefaultDropLocation = "\\build\PrivateDrops"
    $def.RetentionPolicies.Item('Failed').NumberToKeep = 5
    $def.RetentionPolicies.Item('Stopped').NumberToKeep = 0 #None
    $def.RetentionPolicies.Item('PartiallySucceeded').NumberToKeep = 5
    $def.RetentionPolicies.Item('Succeeded').NumberToKeep = 5
    $def.Save()

    Best Regards, 


    Cathy Kong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 8, 2013 1:02 AM
    Moderator