public static void Main(string[] args) { //Configure your Directory Connection here: const string directoryName = "domain.com"; const string directoryContainer = "DC=Domain,DC=Com"; var directory = new PrincipalContextFull(ContextType.Domain, directoryName, directoryContainer); Console.WriteLine("Connected to LDAP://{0}/{1}\n...\n", directoryName, directoryContainer); var i = 0; foreach (var user in directory.GetChildUserObjects()) { i++; try { Console.WriteLine("{0}. Found {1}", i, user.DistinguishedName); user.ExpirePasswordNow(); user.RefreshExpiredPassword(); Console.WriteLine("{0}. PwdLastSet is now refreshed for {1}", i, user.DistinguishedName); } catch (Exception ex) { Console.WriteLine(ex); } } }
public static bool IsEmailUnique(string emailQuery, bool searchOtherDomains) { var domains = new List <Domain>(); if (searchOtherDomains) { domains.AddRange(Forest.GetCurrentForest().Domains.Cast <Domain>()); } else { domains.Add(Domain.GetCurrentDomain()); } foreach (var d in domains) { try { var ad = new PrincipalContextFull(ContextType.Domain, d.Name); var filter = new UserPrincipalFull(ad) { TargetAddress = emailQuery }; var ps = new PrincipalSearcher(filter); var results = ps.FindAll(); if (results.Any()) { return(false); } filter = new UserPrincipalFull(ad) { ProxyAddresses = new List <string> { emailQuery } }; ps = new PrincipalSearcher(filter); results = ps.FindAll(); if (results.Any()) { return(false); } } catch { } } return(true); }
public static List <GenericDirectoryObject> AddressSearch(string emailQuery) { var _results = new List <GenericDirectoryObject>(); foreach (Domain d in Forest.GetCurrentForest().Domains) { try { var ad = new PrincipalContextFull(ContextType.Domain, d.Name); var filter = new GenericDirectoryObject(ad) { TargetAddress = string.Format("smtp:{0}", emailQuery) }; var ps = new PrincipalSearcher(filter); var results = ps.FindAll(); _results.AddRange( results.Select(result => GenericDirectoryObject.FindByIdentity(ad, result.DistinguishedName))); filter = new GenericDirectoryObject(ad) { ProxyAddresses = new List <string> { string.Format("smtp:{0}", emailQuery) } }; ps = new PrincipalSearcher(filter); results = ps.FindAll(); _results.AddRange( results.Select(result => GenericDirectoryObject.FindByIdentity(ad, result.DistinguishedName))); filter = new GenericDirectoryObject(ad) { EmailAddress = emailQuery }; ps = new PrincipalSearcher(filter); results = ps.FindAll(); _results.AddRange( results.Select(result => GenericDirectoryObject.FindByIdentity(ad, result.DistinguishedName))); } catch { } } IEnumerable <GenericDirectoryObject> unsorted = _results; unsorted = unsorted.Distinct(); return(unsorted.ToList()); }
private static void Main(string[] args) { try { foreach (var line in File.ReadLines(args[0])) { try { line.Trim(); if (string.IsNullOrWhiteSpace(line)) { continue; } Console.WriteLine("Reading: {0}", line); string name = string.Empty, email = string.Empty, display = string.Empty, first = string.Empty, last = string.Empty, description = string.Empty; var s = line.Split(new char[1] { ',' }); try { name = s[0]; } catch { } try { email = s[1]; } catch { } try { display = s[0]; } catch { } try { first = s[0]; } catch { } try { last = s[0]; } catch { } try { description = s[0]; } catch { } Console.WriteLine("Name: {0}", name); Console.WriteLine("Email: {0}", email); Console.WriteLine("Display: {0}", display); Console.WriteLine("First: {0}", first); Console.WriteLine("Last: {0}", last); Console.WriteLine("Description: {0}", description); var AD = new PrincipalContextFull(ContextType.Domain, TargetDomain, TargetOu); try { ContactPrincipal.CreateContact(AD, name); } catch { } var contact = ContactPrincipal.FindByIdentity(AD, name); Console.WriteLine("Processing {0} ...", contact.Name); contact.EmailAddress = email; try { contact.Save(); } catch { contact = ContactPrincipal.FindByIdentity(AD, name); } contact.DisplayName = display; try { contact.Save(); } catch { contact = ContactPrincipal.FindByIdentity(AD, name); } contact.Description = description; try { contact.Save(); } catch { contact = ContactPrincipal.FindByIdentity(AD, name); } contact.GivenName = first; try { contact.Save(); } catch { contact = ContactPrincipal.FindByIdentity(AD, name); } contact.Surname = last; try { contact.Save(); } catch { contact = ContactPrincipal.FindByIdentity(AD, name); } try { ExchangeCommon.ExchangeCommon.MailEnable( GenericDirectoryObject.FindByIdentity(AD, contact.DistinguishedName), false); } catch (Exception ex) { Console.WriteLine(ex); } try { ExchangeCommon.ExchangeCommon.AddRoomAttributes(GenericDirectoryObject.FindByIdentity(AD, contact.DistinguishedName)); } catch (Exception ex) { Console.WriteLine(ex); } } catch (Exception ex) { Console.WriteLine(ex); } //Console.Write("Enter to continue"); //Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex); } }
private static void Main(string[] args) { try { var max = int.MaxValue; try { max = int.Parse(args[0]); } catch { } var domain = string.Empty; try { domain = args[1]; } catch { } foreach (Domain d in Forest.GetCurrentForest().Domains) { var directory = new PrincipalContextFull(ContextType.Domain, d.Name); if (!string.IsNullOrWhiteSpace(domain)) { directory = new PrincipalContextFull(ContextType.Domain, domain); } Console.WriteLine("Fetching {0} objects from {1}", max, directory.ConnectedServer); foreach (var gdo in directory.GetAllChildObjects(max)) { try { Console.WriteLine("Found {0} ...", gdo.Name); var email = string.Empty; try { email = gdo.EmailAddress; } catch { } if (string.IsNullOrWhiteSpace(email)) { ExchangeCommon.ExchangeCommon.MailDisable(gdo); } else { ExchangeCommon.ExchangeCommon.MailEnable(gdo, false); } } catch (Exception _ex) { Console.WriteLine("Exception Processing User: {0}", _ex); } finally { Console.WriteLine(); } } } } catch (Exception ex) { Console.WriteLine("General Exception: {0}", ex); } }
public static List <GenericDirectoryObject> FixGalCollision(string identity, string address, bool updateManager) { var domains = new List <Domain>(); var ad = new PrincipalContextFull(ContextType.Domain); foreach (Domain d in Forest.GetCurrentForest().Domains) { try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); if (!string.IsNullOrWhiteSpace(ad.ConnectedServer)) { domains.Add(d); } } catch { } } var authorizedIdentity = new GenericDirectoryObject(ad); foreach (var d in domains) { try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); authorizedIdentity = GenericDirectoryObject.FindByIdentity(ad, identity); if (!string.IsNullOrWhiteSpace(authorizedIdentity.DistinguishedName)) { break; } } catch { } } if (string.IsNullOrWhiteSpace(authorizedIdentity.DistinguishedName)) { throw new ArgumentException("Invalid identity - not found: {0}", identity); } IEnumerable <Principal> results = null; foreach (var d in domains) { try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); var filter = new GenericDirectoryObject(ad) { EmailAddress = address }; var ps = new PrincipalSearcher(filter); results = results == null?ps.FindAll() : results.Concat(ps.FindAll()); filter = new GenericDirectoryObject(ad) { ProxyAddresses = new List <string> { string.Format("smtp:{0}", address) } }; ps = new PrincipalSearcher(filter); results = results.Concat(ps.FindAll()); filter = new GenericDirectoryObject(ad) { TargetAddress = string.Format("smtp:{0}", address) }; ps = new PrincipalSearcher(filter); results = results.Concat(ps.FindAll()); } catch { } } var matches = new List <GenericDirectoryObject>(); foreach (var result in results) { var gdo = new GenericDirectoryObject(ad); foreach (var d in domains) { try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); gdo = GenericDirectoryObject.FindByIdentity(ad, result.DistinguishedName); if (!string.IsNullOrWhiteSpace(gdo.DistinguishedName)) { break; } } catch { } } matches.Add(gdo); } matches.Sort(); var prevDN = string.Empty; var sortedMatches = new List <GenericDirectoryObject>(); foreach ( var _gdo in matches.Where(_gdo => prevDN.ToLowerInvariant() != _gdo.DistinguishedName.ToLowerInvariant())) { prevDN = _gdo.DistinguishedName; sortedMatches.Add(_gdo); } IEnumerable <GenericDirectoryObject> iMatches = sortedMatches; iMatches.Distinct().OrderBy(x => x); foreach (var gdo in iMatches) { if (gdo.DistinguishedName.ToLowerInvariant() == authorizedIdentity.DistinguishedName.ToLowerInvariant()) { MailEnable(gdo, false, false); } else { MailDisable(gdo); if (updateManager) { try { gdo.Manager = authorizedIdentity.DistinguishedName; gdo.Save(); } catch { } } } } return(AddressSearch(address)); }
private static void Main(string[] args) { var ad = new PrincipalContextFull(ContextType.Domain); try { Console.WriteLine(); var email = args[0]; if (email.Split(new[] { '@' }).Count() < 2) { try { foreach (Domain d in Forest.GetCurrentForest().Domains) { Console.WriteLine("Searching in Domain: {0}", d.Name); try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); var gdo = GenericDirectoryObject.FindByIdentity(ad, email); if (!string.IsNullOrWhiteSpace(gdo.DistinguishedName)) { Console.WriteLine("Search hit: {0}", gdo.DistinguishedName); if (string.IsNullOrWhiteSpace(gdo.EmailAddress)) { ExchangeCommon.ExchangeCommon.MailDisable(gdo); return; } email = gdo.EmailAddress; Console.WriteLine("\nSearching Active Directory Forest for Email: {0}", email); } } catch { } if (email.Split(new[] { '@' }).Count() >= 2) { break; } } } catch { } } IEnumerable <Principal> results = null; string manager = null; foreach (Domain d in Forest.GetCurrentForest().Domains) { ad = new PrincipalContextFull(ContextType.Domain, d.Name); var filter = new GenericDirectoryObject(ad) { EmailAddress = email }; var ps = new PrincipalSearcher(filter); results = results == null?ps.FindAll() : results.Concat(ps.FindAll()); filter = new GenericDirectoryObject(ad) { ProxyAddresses = new List <string> { string.Format("smtp:{0}", email) } }; ps = new PrincipalSearcher(filter); results = results.Concat(ps.FindAll()); filter = new GenericDirectoryObject(ad) { TargetAddress = string.Format("smtp:{0}", email) }; ps = new PrincipalSearcher(filter); results = results.Concat(ps.FindAll()); } var matches = new List <GenericDirectoryObject>(); foreach (var result in results) { var gdo = new GenericDirectoryObject(ad); foreach (Domain d in Forest.GetCurrentForest().Domains) { try { ad = new PrincipalContextFull(ContextType.Domain, d.Name); gdo = GenericDirectoryObject.FindByIdentity(ad, result.DistinguishedName); if (!string.IsNullOrWhiteSpace(gdo.DistinguishedName)) { break; } } catch { } } matches.Add(gdo); } matches.Sort(); var previousDn = string.Empty; var sortedMatches = new List <GenericDirectoryObject>(); foreach ( var gdo in matches.Where(gdo => previousDn.ToLowerInvariant() != gdo.DistinguishedName.ToLowerInvariant())) { previousDn = gdo.DistinguishedName; sortedMatches.Add(gdo); } IEnumerable <GenericDirectoryObject> iMatches = sortedMatches; iMatches.Distinct().OrderBy(x => x); Console.WriteLine("\nFound {0} results", iMatches.Count()); foreach (var gdo in iMatches) { Console.WriteLine(gdo.DistinguishedName); } Console.WriteLine(); foreach (var gdo in iMatches) { Console.WriteLine("Match: {0} \n - SamID: {4}\n -Display: {1}\n -GUID: {2}\n -DN: {3}", gdo.Name, gdo.DisplayName, gdo.Guid, gdo.DistinguishedName, gdo.SamAccountName); if (manager == null || gdo.ObjectClass.Contains("contact")) { var n = gdo.DistinguishedName.Split(new[] { ',' })[0].Substring(3).ToLowerInvariant(); var e = email.Split(new[] { '@' })[0].ToLowerInvariant(); if (n == e) { Console.Write("Correct? (Y) "); try { if (Console.ReadLine().Substring(0, 1).ToLowerInvariant() == "n") { ExchangeCommon.ExchangeCommon.MailDisable(gdo); } if (Console.ReadLine().Substring(0, 1).ToLowerInvariant() == "y") { ExchangeCommon.ExchangeCommon.MailEnable(gdo, false); manager = gdo.DistinguishedName; Console.WriteLine(); continue; } } catch { ExchangeCommon.ExchangeCommon.MailEnable(gdo, false); manager = gdo.DistinguishedName; Console.WriteLine(); continue; } } else { Console.Write("Correct? (N) "); try { if (Console.ReadLine().Substring(0, 1).ToLowerInvariant() == "y") { ExchangeCommon.ExchangeCommon.MailEnable(gdo, false); manager = gdo.DistinguishedName; Console.WriteLine(); continue; } if (Console.ReadLine().Substring(0, 1).ToLowerInvariant() == "n") { ExchangeCommon.ExchangeCommon.MailDisable(gdo); } } catch { ExchangeCommon.ExchangeCommon.MailDisable(gdo); } } } else { ExchangeCommon.ExchangeCommon.MailDisable(gdo); } Console.WriteLine(); } if (string.IsNullOrWhiteSpace(manager)) { return; } foreach (var gdo in iMatches.Where(gdo => gdo.DistinguishedName != manager)) { try { var existingMgr = string.Empty; try { existingMgr = gdo.Manager; } catch { } if (!string.IsNullOrWhiteSpace(existingMgr)) { continue; } gdo.Manager = manager; gdo.Save(); } catch { } } } catch (Exception ex) { Console.WriteLine(ex); } }