예제 #1
0
        public static void AddResourceAttributes(GenericDirectoryObject directoryObject)
        {
            try
            {
                var showInAddressBook = directoryObject.ShowInAddressBook;
                showInAddressBook.AddRange(Config.ShowInAddressBookResources);
                directoryObject.ShowInAddressBook = showInAddressBook;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.MsExchResourceDisplay     = "Equipment";
                directoryObject.MsExchResourceDisplayType = 8;

                var MsExchResourceMetaData = directoryObject.MsExchResourceMetaData;
                MsExchResourceMetaData.Add("ResourceType:Equipment");
                directoryObject.MsExchResourceMetaData = MsExchResourceMetaData;

                var MsExchResourceSearchProperties = directoryObject.MsExchResourceSearchProperties;
                MsExchResourceSearchProperties.Add("Equipment");
                directoryObject.MsExchResourceSearchProperties = MsExchResourceSearchProperties;

                directoryObject.Save();
            }
            catch
            {
            }
        }
예제 #2
0
        public List <GenericDirectoryObject> GetAllChildObjects(int maxRecords)
        {
            var directoryDe = new DirectoryEntry();

            if (ContextType == ContextType.ApplicationDirectory)
            {
                if (!string.IsNullOrEmpty(Container) &&
                    !string.IsNullOrEmpty(Name))
                {
                    directoryDe = new DirectoryEntry(string.Format("LDAP://{0}/{1}", Name, Container));
                }
                else
                {
                    directoryDe = new DirectoryEntry(string.Format("LDAP://{0}", Name));
                }
            }
            if (ContextType == ContextType.Domain)
            {
                directoryDe = new DirectoryEntry(string.Format("LDAP://{0}", ConnectedServer));
            }
            if (ContextType == ContextType.Machine)
            {
                throw new NotSupportedException(
                          "This functionality is not available for Machine Context Type PrincipalContext objects.");
            }
            var search = new DirectorySearcher(directoryDe)
            {
                Tombstone    = false,
                Asynchronous = true,
                PageSize     = 100,
                Filter       = "(objectClass=Top)"
            };
            var results = search.FindAll();

            var i        = 0;
            var children = new List <GenericDirectoryObject>();

            foreach (SearchResult result in results)
            {
                i++;
                var delims = new[] { '/' };
                var pieces = result.Path.Split(delims);
                var dn     = pieces[pieces.Count() - 1];
                if (maxRecords > 0 && i > maxRecords)
                {
                    break;
                }
                try
                {
                    children.Add(GenericDirectoryObject.FindByIdentity(this, IdentityType.DistinguishedName, dn));
                }
                catch
                {
                }
            }
            return(children);
        }
예제 #3
0
        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());
        }
예제 #4
0
        public static List <string> GetSmtpAliases(GenericDirectoryObject directoryObject)
        {
            var Addresses = new List <string> {
                directoryObject.EmailAddress
            };

            foreach (var proxyAddress in directoryObject.ProxyAddresses)
            {
                if (proxyAddress.Substring(0, 5).ToLowerInvariant() == "smtp:".ToLowerInvariant())
                {
                    Addresses.Add(proxyAddress.Substring(5));
                }
            }
            IEnumerable <string> unsorted = Addresses;

            unsorted = unsorted.Distinct();
            return(unsorted.ToList());
        }
예제 #5
0
 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);
     }
 }
예제 #6
0
        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));
        }
예제 #7
0
        public static void MailDisable(GenericDirectoryObject directoryObject)
        {
            try
            {
                directoryObject.EmailAddress  = null;
                directoryObject.TargetAddress = null;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.MailNickname = null;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            var proxyAddresses = new List <string>();

            try
            {
                proxyAddresses.AddRange(
                    directoryObject.ProxyAddresses.Where(
                        address => address.Substring(0, 5).ToLowerInvariant() != "smtp:"));
            }
            catch
            {
            }
            try
            {
                if (proxyAddresses.Count > 0)
                {
                    directoryObject.ProxyAddresses = proxyAddresses;
                    directoryObject.Save();
                }
                else
                {
                    directoryObject.SetAttribute("proxyAddresses", null);
                    directoryObject.Save();
                }
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.SetAttribute("showInAddressBook", null);
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.LegacyExchangeDn = null;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
        }
예제 #8
0
        public static void AddAlias(GenericDirectoryObject directoryObject, string address, bool makeDefault)
        {
            var handle          = address.Split(new[] { '@' })[0];
            var proxyAddresses  = new List <string>();
            var nicknameProxies = false;

            if (makeDefault)
            {
                try
                {
                    directoryObject.EmailAddress  = address.Trim();
                    directoryObject.TargetAddress = string.Format("smtp:{0}", address);
                    if (string.IsNullOrWhiteSpace(directoryObject.MailNickname))
                    {
                        directoryObject.MailNickname = handle;
                        nicknameProxies = true;
                    }
                    directoryObject.Save();
                }
                catch
                {
                    directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                            directoryObject.DistinguishedName);
                }
            }
            try
            {
                proxyAddresses = directoryObject.ProxyAddresses;
            }
            catch
            {
            }
            var newProxyAddresses = new List <string>();

            foreach (var proxyAddress in proxyAddresses)
            {
                if (proxyAddress.Substring(0, 5).ToLowerInvariant() == "smtp:")
                {
                    var s = proxyAddress.Split(new[] { ':' });
                    if (s[1].ToLowerInvariant() == address.ToLowerInvariant())
                    {
                        newProxyAddresses.Add(makeDefault
                            ? string.Format("SMTP:{0}", address)
                            : string.Format("smtp:{0}", address));
                    }
                    else
                    {
                        newProxyAddresses.Add(proxyAddress.Replace("SMTP", "smtp"));
                    }
                }
                else
                {
                    newProxyAddresses.Add(proxyAddress);
                }
            }
            if (nicknameProxies)
            {
                newProxyAddresses.AddRange(
                    Config.RoutingDomains.Select(domain => string.Format("smtp:{0}@{1}", handle, domain)));
            }
            try
            {
                directoryObject.SetAttribute("proxyAddresses", null);
                directoryObject.Save();
            }
            catch
            {
            }
            finally
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            directoryObject.ProxyAddresses = newProxyAddresses;
            directoryObject.Save();
        }
예제 #9
0
        public static void MailEnable(GenericDirectoryObject directoryObject, bool generateFriendlyAddress,
                                      bool verifyUniqueness)
        {
            var email       = string.Empty;
            var emailDomain = string.Empty;
            var handle      = string.Empty;

            try
            {
                handle = directoryObject.SamAccountName;
            }
            catch
            {
            }
            if (string.IsNullOrWhiteSpace(handle))
            {
                handle = directoryObject.Name;
            }
            try
            {
                email = directoryObject.EmailAddress;
            }
            catch
            {
            }
            try
            {
                emailDomain = email.Split(new[] { '@' })[1];
            }
            catch
            {
            }
            try
            {
                if (email.Trim() != directoryObject.EmailAddress)
                {
                    directoryObject.EmailAddress = email.Trim();
                    directoryObject.Save();
                    email = directoryObject.EmailAddress;
                }
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            if (string.IsNullOrWhiteSpace(email))
            {
                return;
            }
            string friendlyEmail;

            if (IsManagedDomain(emailDomain))
            {
                //firstname.lastname@domain
                friendlyEmail = string.Format("smtp:{0}@{1}",
                                              directoryObject.DisplayName.Replace(" ", ".").Replace("'", ""), emailDomain);
                if (!verifyUniqueness || !IsEmailUnique(friendlyEmail, directoryObject.DistinguishedName))
                {
                    //flastname@domain
                    friendlyEmail = string.Format("smtp:{0}@{1}",
                                                  directoryObject.DisplayName.Substring(0, 1) + directoryObject.Surname, emailDomain);
                    if (!verifyUniqueness || !IsEmailUnique(friendlyEmail, directoryObject.DistinguishedName))
                    {
                        //userID@domain
                        friendlyEmail = string.Format("smtp:{0}@{1}", handle, emailDomain);
                    }
                }
            }
            else
            {
                friendlyEmail = string.Format("smtp:{0}", email);
            }
            try
            {
                if (generateFriendlyAddress)
                {
                    directoryObject.TargetAddress = friendlyEmail;
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(directoryObject.SamAccountName))
                    {
                        directoryObject.TargetAddress = IsManagedDomain(emailDomain)
                            ? string.Format("smtp:{0}@{1}", handle, emailDomain)
                            : string.Format("smtp:{0}", directoryObject.EmailAddress);
                    }
                    else
                    {
                        directoryObject.TargetAddress = directoryObject.ObjectClass.Contains("contact")
                            ? string.Format("smtp:{0}", directoryObject.EmailAddress)
                            : string.Format("smtp:{0}@{1}", directoryObject.Name, emailDomain);
                    }
                }
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.MailNickname = handle;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                var proxyAddresses = new List <string>();
                try
                {
                    proxyAddresses = directoryObject.ProxyAddresses;
                }
                catch
                {
                }
                proxyAddresses.Add(string.Format("SMTP:{0}", directoryObject.EmailAddress));
                proxyAddresses.AddRange(
                    Config.RoutingDomains.Select(domain => string.Format("smtp:{0}@{1}", handle, domain)));
                proxyAddresses.AddRange(
                    Config.RoutingDomains.Select(
                        domain => string.Format("{0}@{1}", friendlyEmail.Split(new char[1] {
                    '@'
                })[0], domain)));
                directoryObject.ProxyAddresses = proxyAddresses;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                var showInAddressBook = new List <string>();
                try
                {
                    showInAddressBook = directoryObject.ShowInAddressBook;
                }
                catch
                {
                }
                showInAddressBook.AddRange(Config.ShowInAddressBookDefault);
                directoryObject.ShowInAddressBook = showInAddressBook;
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                //will require editing for your environment or modifying this to be configurable:
                directoryObject.LegacyExchangeDn = string.Format("/o=Company/ou=Legacy/cn=Recipients/cn={0}",
                                                                 directoryObject.SamAccountName);
                directoryObject.Save();
            }
            catch
            {
                directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context,
                                                                        directoryObject.DistinguishedName);
            }
            try
            {
                directoryObject.UserPrincipalName = string.Format("{0}@{1}",
                                                                  directoryObject.UserPrincipalName.Split(new[] { '@' })[0], Config.UpnSuffix);
                directoryObject.Save();
            }
            catch
            {
            }
        }
예제 #10
0
 public static void MailEnable(GenericDirectoryObject directoryObject, bool generateFriendlyAddress)
 {
     MailEnable(directoryObject, generateFriendlyAddress, true);
 }
예제 #11
0
        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);
            }
        }