简体   繁体   English

C#如何从Active Directory中的每个部门获取组列表

[英]C# How to get a list of groups from each department in Active Directory

I've made a small application that reads out all the departments in our AD, and saves it as an xml file for each company that the departments belong to. 我创建了一个小应用程序,它读取了AD中的所有部门,并将其保存为部门所属的每个公司的xml文件。 But I also need to add all the groups that users in each department in each company is member of. 但我还需要添加每个公司中每个部门的用户所属的所有组。

Here is a snippet of one of the xml files that my code produces. 这是我的代码生成的一个xml文件的片段。 I want to add the groups as a child to each department node. 我想将这些组作为子项添加到每个部门节点。

<departments>
  <department name="Administrasjon">
    <group></group>
  </department>
  <department name="Barnehage">
    <group></group>
  </department>
  <department name="Bibliotek">
    <group></group>
  </department>
  <department name="Frivilligsentralen">
    <group></group>
  </department>
</departmets>

The code for generating and saving this is shown below. 生成和保存它的代码如下所示。 Is it possible to get what i want? 有可能得到我想要的吗? Any help would be appreciated. 任何帮助,将不胜感激。

static void Main(string[] args)
        {
            ArrayList companies = new ArrayList();
            companies = GetCompaniesFromAD();
            for (int i = 0; i < companies.Count; i++)
            {
                GetCompanyAndDepAndCreateXML( companies[i].ToString() );
            }                
        }

        public static ArrayList GetCompaniesFromAD()
        {         
            string path = "LDAP://myserver/OU=Brukere,OU=Felles,DC=bla,DC=bla,DC=bla";


            DirectoryEntry entry = new DirectoryEntry(path);
            ArrayList companies = new ArrayList();
            string sFilter = String.Format("(&(objectCategory=Person)(objectClass=user)(company=*))");
            string[] attribs = new string[] { "department", "company" };

            using (DirectorySearcher dirsearcher = new DirectorySearcher(entry,sFilter,attribs))
            {                
                foreach (SearchResult sResult in dirsearcher.FindAll())
                {
                    StringBuilder companyNames = new StringBuilder();

                    if(sResult.Properties.Contains("company"))
                    {
                        foreach (object o in sResult.Properties["company"])
                        {
                            companyNames.AppendFormat("{0}", o);
                            companies.Add(o.ToString());                            
                        }                        
                    } //end if "company                    

                } //end foreach                
            } //end using..



            ArrayList companyNoDuplicates = new ArrayList();
            companyNoDuplicates = RemoveDuplicates(companies);
            return companyNoDuplicates;
          }

        public static void GetCompanyAndDepAndCreateXML(string companyName)
        {
            string path = "LDAP://myserver/OU=Brukere,OU=Felles,DC=bla,DC=bla,DC=bla";


            DirectoryEntry entry = new DirectoryEntry(path);            
            ArrayList departments = new ArrayList();
            string sFilter = String.Format("(&(objectCategory=Person)(objectClass=user)(company=" + companyName + "))");
            string[] attribs = new string[] { "department" };

            using (DirectorySearcher dirsearcher = new DirectorySearcher(entry, sFilter, attribs))
            {
                foreach (SearchResult sResult in dirsearcher.FindAll())
                {                    
                    StringBuilder departmentNames = new StringBuilder();                    

                    if (sResult.Properties.Contains("department"))
                    {
                        foreach (object o in sResult.Properties["department"])
                        {
                            departmentNames.AppendFormat("{0}", o);
                            departments.Add(o.ToString());                            
                        }
                    } //end if department
                } //end foreach                
            } //end using..


            ArrayList departmentNoDuplicates = new ArrayList();
            departmentNoDuplicates = RemoveDuplicates(departments);                        

            generateXmlDoc(departmentNoDuplicates, companyName);
        }

        public static void generateXmlDoc(ArrayList departments, string companyName)
        {
            XElement xml = new XElement("departments");
            for (int i = 0; i < departments.Count; i++)
            {
                XElement node = new XElement("department",
                    new XAttribute("name", departments[i].ToString()),
                    new XElement("group", "")
                    );
                xml.Add(node);
            }
            xml.Save(companyName + ".xml");

        }

        public static ArrayList RemoveDuplicates(ArrayList items)
        {
            ArrayList noDuplicates = new ArrayList();
            foreach (string strItem in items)
            {
                if (!noDuplicates.Contains(strItem.Trim()))
                {
                    noDuplicates.Add(strItem.Trim());
                }
            }
            noDuplicates.Sort();

            return noDuplicates;
        }

如果您使用System.DirectoryServices.AccountManagement,这将更容易实现,因为它更直观地完成您的任务。

As "SCMcDonnell" said, if you're on .NET 3.5 or higher, definitely use the S.DS.AccountManagement namespace. 正如“SCMcDonnell”所说,如果您使用的是.NET 3.5或更高版本,请务必使用S.DS.AccountManagement命名空间。

Find an excellent article describing this new namespace on MSDN - Joe Kaplan and Ethan Wilansky did a great job explaining the new features and how to use them. MSDN上找到一篇描述这个新命名空间的优秀文章 - Joe Kaplan和Ethan Wilansky在解释新功能以及如何使用它们方面表现出色。

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM