Skip to content

Instantly share code, notes, and snippets.

@mkchandler
Created February 25, 2014 16:32
Show Gist options
  • Save mkchandler/9212462 to your computer and use it in GitHub Desktop.
Save mkchandler/9212462 to your computer and use it in GitHub Desktop.
Gets a list of all available domain controllers or find a single available controller.
using System;
using System.DirectoryServices.ActiveDirectory;
namespace ActiveDomainControllers
{
public class Program
{
public static void Main(string[] args)
{
// Get a list of all available domain contollers
var domainControllers = DomainController.FindAll(new DirectoryContext(DirectoryContextType.Domain, "domain.name"));
foreach (DomainController domainController in domainControllers)
{
Console.WriteLine(domainController.Name);
}
// Finds a single domain controller that is currently available
var singleDomainController = DomainController.FindOne(new DirectoryContext(DirectoryContextType.Domain, "domain.name"), LocatorOptions.ForceRediscovery);
Console.WriteLine("Single: " + singleDomainController.Name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment