将新OU添加到Active Directory

时间:2015-09-05 19:58:56

标签: c# active-directory

我使用此语法将新OU添加到Active Directory:

var OUname = "VIP"

DirectoryEntry de = new DirectoryEntry("LDAP://MyIp/OU=NEW,OU=MyUsers,DC=MyServer,DC=com");
de.Username = "administrator";
de.Password = "mypassword";
de = de.Children.Add("OU=NameNewOU", "OrganizationalUnit");
de.CommitChanges();

一切正常,但我的问题是如何设置变量而不是OU=NameNewOU

1 个答案:

答案 0 :(得分:1)

你拥有所需的一切。我建议你用html语句包装你的代码,以防止任何内存泄漏。

using

在此示例中,var ouName = "OU=" + "VIP"; //VIP will be the name of OU. using(DirectoryEntry de = new DirectoryEntry("LDAP://MyIp/OU=NEW,OU=MyUsers,DC=MyServer,DC=com")) { de.Username = "administrator"; de.Password = "mypassword"; de = de.Children.Add(ouName, "OrganizationalUnit"); de.CommitChanges(); } 将使用OU名称创建。