Some code to remove old build agent and test environment references


Disclaimer: This is just a blog post, and this isn’t supported code by any stretch of the imagination. It hasn’t been certified by anybody. I can’t say that customer support will help you if you’ve used it.
It’s just me using the TFS API.

I have an unusual situation. Since I demonstrate Visual Studio, TFS, and Lab Management a lot, and I’m a field employee, I have everything running on my laptop. I’ve set up my laptop to run Server 2008 R2 SP1, and TFS is on a VM, as is a domain controller, and some lab environments.

Over the past year, I’ve actually created a new TFS VM, detached the old TFS database, and moved it to the new instance. I’ve also decommissioned an old lab environment and created a new one with nearly the same name. That’s left a few state references in my TFS database. I wanted those old references gone—there are some problems in having services listed that don’t really exist any more.

So, for my build server, I used the TFS API and wrote a console application to find the now-departed build agents and unregister them properly.  In my case, I’m attaching to “mycollection” on “myserver”, and deleting agents on build servers on a machine named “stalehost”.

Delete stale build servers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.TeamFoundation.Client;
  6. using Microsoft.TeamFoundation.Build.Client;
  7. using System.Diagnostics;
  8.  
  9. namespace UnregisterBuildServiceHost
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var tfs = TeamFoundationServerFactory.GetServer("http://myserver:8080/tfs/mycollection");
  16.             IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
  17.  
  18.             var buildServiceHosts = (IEnumerable<IBuildServiceHost>) buildServer.QueryBuildServiceHosts("stalehost*");
  19.             var buildControllers = (IEnumerable<IBuildController>)buildServer.QueryBuildControllers(true);
  20.  
  21.             foreach (IBuildServiceHost buildHost in buildServiceHosts)
  22.             {
  23.                 Debug.WriteLine("{0} URI:({1}) Controller:({2}) Number of agents: {3}", buildHost.Name, buildHost.Uri, buildHost.Controller, buildHost.Agents.Count);
  24.                 foreach (var agent in buildHost.Agents)
  25.                 {
  26.                     agent.Delete();
  27.                 }
  28.                 if (buildHost.Controller != null) buildHost.Controller.Delete();
  29.                 buildHost.Delete();
  30.             }
  31.  
  32.             foreach (IBuildController controller in buildControllers)
  33.             {
  34.                 Debug.WriteLine("{0} ({1})", controller.Name, controller.ServiceHost.Name);
  35.  
  36.                 foreach (IBuildAgent agent in controller.Agents)
  37.                 {
  38.                     Debug.WriteLine("\t{0} ({1}) {2}", agent.Name, agent.ServiceHost.Name, agent.Uri);
  39.                 }
  40.             }
  41.  
  42.  
  43.  
  44.         }
  45.     }
  46. }

The build server installation will also allow you to replace an existing “stale” agent with the one you’re installing. So this is only in the instance that you have a build server that you didn’t unregister and you’re not replacing.  Again, I’m connecting to “mycollection” on “myserver”, to unregister the stale environment “Departed Env”.

Delete stale test environments
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.TeamFoundation.Client;
  7. using Microsoft.TeamFoundation.TestManagement.Client;
  8.  
  9. namespace UnregisterBuildServiceHost
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             TeamFoundationServer tfs;
  16.             ITestManagementService testManagementService;
  17.             tfs = new TeamFoundationServer("http://myserver:8080/tfs/mycollection");
  18.             testManagementService = tfs.GetService<Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService>();
  19.  
  20.             var testControllerHelper = testManagementService.TestControllers;
  21.  
  22.             var testControllers = testControllerHelper.Query();
  23.  
  24.  
  25.             foreach (var testController in testControllers)
  26.             {
  27.                 Debug.WriteLine("Display Name'{0}' :Group ID({1}) Name:({2})", testController.DisplayName, testController.GroupId, testController.Name);
  28.                 var testenvs = testController.TestEnvironments.Query();
  29.                 foreach (var testenv in testenvs)
  30.                 {
  31.                     Debug.WriteLine("\tName: {0}, Desc:({1}) Date Created: ({2}) Environment Type:({3}) Error({4})",
  32.                         testenv.DisplayName, testenv.Description, testenv.DateCreated, testenv.EnvironmentType, testenv.Error);
  33.                     if (testenv.DisplayName.Equals("Departed Env"))
  34.                     {
  35.                         testenv.Unregister();
  36.                     }
  37.                 }
  38.  
  39.             }
  40.  
  41.            
  42.  
  43.         }
  44.     }
  45. }

Now, both of these are single-purpose monolithic console applications. This isn’t elegant code—it’s just a hack to solve a temporary condition. I just thought it was a simple demonstration of the TFS API.

Technorati Tags: ,,,,,,,,,,,,,,,,
Windows Live Tags: TFS API,test agent,test environment,Visual Studio,Test Management,UnregisterBuildServiceHost,TeamFoundationServerFactory,IBuildServer,IBuildServiceHost,QueryBuildServiceHosts,IBuildController,QueryBuildControllers,IBuildAgent,TestManagement,TeamFoundationServer,ITestManagementService,TestControllers


Skip to main content