Skip to content

Instantly share code, notes, and snippets.

@KevinT
Created April 25, 2013 13:53
Show Gist options
  • Save KevinT/5459862 to your computer and use it in GitHub Desktop.
Save KevinT/5459862 to your computer and use it in GitHub Desktop.
Talking to ActiveDirectory using ScriptCS. Purpose: Iterates over a file listing DOMAIN\USER credentials and spits out a file that includes their full names.
#r "System.DirectoryServices"
using System.DirectoryServices;
using System.IO;
var fullNames = new List<string>();
foreach (var credential in File.ReadAllLines("credentials.txt"))
{
if (credential.IndexOf('\\') > 0)
{
var domain = credential.Substring(0, credential.IndexOf('\\'));
var name = credential.Substring(credential.IndexOf('\\') + 1);
var de = new DirectoryEntry("LDAP://" + domain.ToUpper());
var search = new DirectorySearcher(de);
search.Filter = String.Format("(SAMAccountName={0})", name);
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
{
Console.WriteLine("Not Found");
}
else
{
fullNames.Add(string.Format("{0}, {1}", credential, result.Properties["cn"][0].ToString()));
}
}
}
File.WriteAllLines("fullnames.txt", fullNames);
@KevinT
Copy link
Author

KevinT commented Apr 25, 2013

Sample credentials.txt (between the ###):

DOMAINA\abh
DOMAINA\a_nic
DOMAINA\gan
DOMAINB\btv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment