문제

모든 사용자 (로컬 시스템 및 도메인 모두)를 나열해야합니다. WQL을 사용해 보았지만 프로그램이 실행되는 데 많은 시간이 걸립니다. 레지스트리에서 얻을 수있는 다른 방법이 있습니까? 모든 도움이 감사하겠습니다.

도움이 되었습니까?

해결책

using System;
using System.Collections.Generic;
using System.DirectoryServices;
namespace ListADUsers.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();
            IList<String> userList = new List<String>();
            int badEntries = 0;
            string domainName = String.Empty;
            if (args.Length > 0)
                domainName = args[0];
            else
            {
                Console.Write("\nPlease enter your Active Directory domain name: ");
                domainName = Console.ReadLine();
            }
            Console.Write(String.Format("\nAttempting to build user list for {0} ...\n\n", domainName));
            try
            {
                if (!String.IsNullOrEmpty(domainName))
                {
                    DirectoryEntry myDirectoryEntry = new DirectoryEntry(String.Format("LDAP://{0}", domainName));
                    DirectorySearcher mySearcher = new DirectorySearcher(myDirectoryEntry);
                    SortOption mySort = new SortOption("sn", SortDirection.Ascending);
                    mySearcher.Filter = ("(objectCategory=person)");
                    mySearcher.Sort = mySort;
                    foreach (SearchResult resEnt in mySearcher.FindAll())
                    {
                        try
                        {
                            if (!String.IsNullOrEmpty(resEnt.Properties["Mail"][0].ToString())
                                && System.Text.RegularExpressions.Regex.IsMatch(resEnt.Properties["DisplayName"][0].ToString(), " |admin|test|service|system|[$]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                                )
                                {
                                    int space = resEnt.Properties["DisplayName"][0].ToString().IndexOf(" ");
                                    string formattedName = String.Format("{0}{1}{2}",
                                        resEnt.Properties["DisplayName"][0].ToString().Substring(space).PadRight(25),
                                        resEnt.Properties["DisplayName"][0].ToString().Substring(0, space).PadRight(15),
                                        resEnt.Properties["Mail"][0].ToString()
                                        );
                                    userList.Add(formattedName);
                                }
                        }
                        catch
                        {
                            badEntries++;
                        }
                    }
                    if (userList.Count > 0)
                    {
                        Console.WriteLine(String.Format("=========== Listing of users in the {0} domain\n", domainName));
                        Console.WriteLine(String.Format("{0}{1}{2}", "Surname".PadRight(25), "First Name".PadRight(15), "Email Address\n"));
                        for (int i = 0; i < userList.Count - 1; i++)
                            Console.WriteLine(userList[i].ToString());
                        Console.WriteLine(String.Format("\n=========== {0} users found in the {1} domain", userList.Count.ToString(), domainName));
                    }
                    else
                        Console.WriteLine(String.Format("\n=========== 0 users found in the {0} domain", userList.Count.ToString()));
                    Console.WriteLine(String.Format("=========== {0} objects could not be read", badEntries.ToString()));
                    Console.WriteLine("=========== End of Listing");
                }
                else
                {
                    Console.WriteLine("Please enter a domain name next time!");
                }
            }
            catch (Exception ex)
            {
                // in a production app you wouldn't show the user the exception details
                Console.Write(String.Format("A critical error occurred.\nDetails: {0}", ex.Message.ToString()));
            }
        }
    }
}

샘플 응용 프로그램 다운로드 : 광고 사용자를 나열하는 샘플 응용 프로그램

원천 : 후속 조치 - Active Directory 사용자 나열 - 이번에는 C#에 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top