0

I am trying to create a PS script to create TFS build definition files. I found below example. But, it's not working properly. I tried to follow the example and create a script without any success. Any help in identifying why below script is not working is appreciated and also guidance on how to create a PS script or an example

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()
rene
  • 41,474
  • 78
  • 114
  • 152
user1636380
  • 79
  • 1
  • 9

1 Answers1

0

You seem to try to create build definition on tfs 2010 using 2008 assembly and aproach, without provided outputs or errors is hard to tell exact issue, but few thing stand out:

  • use the version of assemblies that match your tfs server (as you tagged question tfs2010)
  • For 2010 suggested way have to work with api is to create TfsTeamProjectCollection object from uri, from there you will use get service as you do now.
  • Your code suggest is for 2008, there have been major changes how tfs builds work in 2010, specificly DefaultBuildAgent property is obsolete and unused, agents are assigned by build controller use following as reference for your script.

http://geekswithblogs.net/jakob/archive/2010/04/26/creating-a-build-definition-using-the-tfs-2010-api.aspx

drk
  • 865
  • 5
  • 9
  • I started on a PS script based on the example. $process = $def.ProcessParameters $def.ProcessParameters = $process $def.RetentionPolicyList.Clear() $def.Save() This give me an error saying "Exception calling "Save" with "0" argument(s): "TF42073: The value cannot be null (Parameter name: Process)" What am I missing here? – user1636380 Aug 16 '13 at 21:11
  • Did you get and assign the process template for your definition as in article i linked ? You may need to do more work to get your custom template. //Get default template var defaultTemplate = buildServer.QueryProcessTemplates(teamProject).Where(p => p.TemplateType == ProcessTemplateType.Default).First(); buildDefinition.Process = defaultTemplate; – drk Sep 10 '13 at 10:38
  • Yes I was able to get it to work. I had few more issues, which were answered by below posts. http://stackoverflow.com/questions/18389222/convert-c-sharp-logic-to-powershell-for-tfs http://stackoverflow.com/questions/18447879/powershell-syntax-error – user1636380 Sep 11 '13 at 13:50