Created
April 25, 2013 13:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample credentials.txt (between the ###):
DOMAINA\abh
DOMAINA\a_nic
DOMAINA\gan
DOMAINB\btv