我创建了一个小应用程序,它读取AD中的所有部门,并将其保存为部门所属的每个公司的xml文件。但我还需要添加每个公司中每个部门的用户都是其成员的所有组。
以下是我的代码生成的一个xml文件的片段。我想将这些组作为子项添加到每个部门节点。
<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>
生成和保存它的代码如下所示。有可能得到我想要的吗? 任何帮助将不胜感激。
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;
}
答案 0 :(得分:2)
如果您使用System.DirectoryServices.AccountManagement,这将更容易实现,因为它更直观地完成您的任务。
答案 1 :(得分:2)
正如“SCMcDonnell”所说,如果您使用的是.NET 3.5或更高版本,请务必使用S.DS.AccountManagement命名空间。
在MSDN上找到一篇描述这个新命名空间的优秀文章 - Joe Kaplan和Ethan Wilansky在解释新功能以及如何使用它们方面做得很好。
马克