Skip to content

Instantly share code, notes, and snippets.

@rfennell
Last active December 29, 2016 21:46
Show Gist options
  • Save rfennell/27b7e493d5c4d7558ea9e6edda9d473e to your computer and use it in GitHub Desktop.
Save rfennell/27b7e493d5c4d7558ea9e6edda9d473e to your computer and use it in GitHub Desktop.
Tool to remove a XAML build controller from a VSTS instance
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using System;
using System.Linq;
// Needs the Nuget package Microsoft.TeamFoundationServer.ExtendedClient
namespace RemoveXAMLBuildController
{
class Program
{
static void Main(string[] args)
{
if (args.Count() != 3)
{
Console.WriteLine("Usage: RemoveXAMLBuildController \"https://<Your instance>.visualstudio.com\" \"<Your PAT>\" \"Controller Name\"");
}
else
{
RemoveXAMLBuildController(new Uri(args[0]), args[1], args[2]);
}
}
private static void RemoveXAMLBuildController(Uri uri, string pat, string name)
{
System.Net.NetworkCredential networkCredential = new
System.Net.NetworkCredential("username", pat); // any username is OK
BasicAuthCredential basicCredential = new BasicAuthCredential(networkCredential);
TfsClientCredentials tfsCredentials = new TfsClientCredentials(basicCredential);
tfsCredentials.AllowInteractive = false;
var server = new TfsTeamProjectCollection(uri, tfsCredentials);
server.Authenticate();
var buildServer = (IBuildServer)server.GetService(typeof(IBuildServer));
var controller = buildServer.QueryBuildControllers().FirstOrDefault(c => c.Name.Equals(name));
if (controller == null)
{
Console.WriteLine($"No XAML controllers found with name '{name}'");
}
else
{
if (controller.Agents.Count > 0)
{
Console.WriteLine("Deleting build agents in controller");
buildServer.DeleteBuildAgents(controller.Agents.ToArray());
}
Console.WriteLine($"Deleting build controller '{name}'");
buildServer.DeleteBuildControllers(new IBuildController[] { controller });
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment