无法获取我的 AD 中某些组的属性

发布于 2024-09-15 04:16:01 字数 2518 浏览 3 评论 0原文

我正在尝试从公司的 AD 系统中检索所有电子邮件组及其邮件地址。我有大约 1800 个组,但我发现有大约 20 个组我无法获取它们的属性。我在 Outlook 中进行了尝试,并正确获取了邮件地址等属性。但我无法通过代码获取它们,请有人帮忙。谢谢。下面是我的代码片段:

    static void TestGroupEmails()
    {
        ICollection<DirectoryEntry> groups = GetGroups();
        Console.WriteLine(groups.Count + "groups");
        List<String> noNameGroups = new List<String>();
        foreach (DirectoryEntry de in groups)
        {
            String name = de.Properties["sAMAccountName"].Value as String;
            String email = de.Properties["mail"].Value as String;
            if (String.IsNullOrEmpty(email))
                noNameGroups.Add(name);
        }
        StreamWriter writer = new StreamWriter(@"C:\ad\group mails.txt");
        noNameGroups.Sort();
        foreach (String name in noNameGroups)
        {
            writer.WriteLine(name);
        }
        writer.Close();
        Console.ReadLine();
    }

    public static List<DirectoryEntry> GetGroups()
    {
        String filter = @"(&(objectCategory=group))";

        List<DirectoryEntry> groups = new List<DirectoryEntry>();

        using (DirectoryEntry root = new DirectoryEntry(Constants.ADConnPrefix))
        {
            using (DirectorySearcher searcher = new DirectorySearcher(filter, null))
            {
                searcher.PageSize = 10000;

                searcher.ReferralChasing = ReferralChasingOption.All;

                searcher.SearchScope = SearchScope.Subtree;

                searcher.SearchRoot = root;

                root.Username = Constants.UserName;
                root.Password = Constants.Password;

                using (SearchResultCollection searchResult = searcher.FindAll())
                {
                    foreach (SearchResult sr in searchResult)
                    {

                        DirectoryEntry de = sr.GetDirectoryEntry();
                        groups.Add(de);

                    }
                }
            }
        }
        return groups;
    }

    public static SearchResult GetGroupInfo(String groupName)
    {
        String normalName = Utility.RemoveLoginNamePrefix(groupName);
        String filterFormat = "(&(objectCategory=group)(sAMAccountName={0}))";
        using (SearchResultCollection searchResult = Search(ADConnPrefix, null, filterFormat, normalName))
        {
            int count = searchResult.Count;
            SearchResult sr = searchResult[0];
            return sr;
        }
    }

I am trying to retrieve all the email gruops and their mail address from company's AD system. I've got about 1800 groups but I've found there are about 20 gourps which I cannot get their properties. I tried in my outlook and got properties like mail address correctly. But I cannot get them by code, someone please help. Thanks. Below is my code snippet:

    static void TestGroupEmails()
    {
        ICollection<DirectoryEntry> groups = GetGroups();
        Console.WriteLine(groups.Count + "groups");
        List<String> noNameGroups = new List<String>();
        foreach (DirectoryEntry de in groups)
        {
            String name = de.Properties["sAMAccountName"].Value as String;
            String email = de.Properties["mail"].Value as String;
            if (String.IsNullOrEmpty(email))
                noNameGroups.Add(name);
        }
        StreamWriter writer = new StreamWriter(@"C:\ad\group mails.txt");
        noNameGroups.Sort();
        foreach (String name in noNameGroups)
        {
            writer.WriteLine(name);
        }
        writer.Close();
        Console.ReadLine();
    }

    public static List<DirectoryEntry> GetGroups()
    {
        String filter = @"(&(objectCategory=group))";

        List<DirectoryEntry> groups = new List<DirectoryEntry>();

        using (DirectoryEntry root = new DirectoryEntry(Constants.ADConnPrefix))
        {
            using (DirectorySearcher searcher = new DirectorySearcher(filter, null))
            {
                searcher.PageSize = 10000;

                searcher.ReferralChasing = ReferralChasingOption.All;

                searcher.SearchScope = SearchScope.Subtree;

                searcher.SearchRoot = root;

                root.Username = Constants.UserName;
                root.Password = Constants.Password;

                using (SearchResultCollection searchResult = searcher.FindAll())
                {
                    foreach (SearchResult sr in searchResult)
                    {

                        DirectoryEntry de = sr.GetDirectoryEntry();
                        groups.Add(de);

                    }
                }
            }
        }
        return groups;
    }

    public static SearchResult GetGroupInfo(String groupName)
    {
        String normalName = Utility.RemoveLoginNamePrefix(groupName);
        String filterFormat = "(&(objectCategory=group)(sAMAccountName={0}))";
        using (SearchResultCollection searchResult = Search(ADConnPrefix, null, filterFormat, normalName))
        {
            int count = searchResult.Count;
            SearchResult sr = searchResult[0];
            return sr;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-09-22 04:16:01

您确定相关群组确实有电子邮件地址吗? AD 中的群体可能没有它们。

如果您将搜索过滤器修改为 (&(objectCategory=group)(mail=*)),它将过滤掉任何没有电子邮件地址的群组。

Are you certain the groups in question actually have email addresses? It is possible for groups in AD to not have them.

If you modify your search filter to (&(objectCategory=group)(mail=*)) it will filter out any groups that don't have email addresses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文