【求助】如何用C#编程方式批量对域控制器添加OU

wqb1979 2005-10-26 11:50:40
现在单位的部门(包括子部门)有上百个,之前做了一个管理系统已经有把机构存入数据库了,层次结构都有了,能否用编程方式读取数据库的数据,然后对应在域中添加OU,免得一个一个手动输入?

不够分可以再加。
...全文
460 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
spgoal 2005-11-09
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adam/adam/creating_organizational_units.asp

The following C# code example in uses the DirectoryEntry constructor to bind to an organization object and uses the Add method to create an organizationalUnit object in that organization.


using System;
using System.DirectoryServices;

namespace ADAM_Examples
{
class CreateOU
{
/// <summary>
/// Create ADAM Organizational Unit.
/// </summary>
[STAThread]
static void Main()
{
DirectoryEntry objADAM; // Binding object.
DirectoryEntry objOU; // Organizational unit.
string strDescription; // Description of OU.
string strOU; // Organiztional unit.
string strPath; // Binding path.

// Construct the binding string.
strPath = "LDAP://localhost:389/O=Fabrikam,C=US";

Console.WriteLine("Bind to: {0}", strPath);

// Get ADAM object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.RefreshCache();
}
catch (Exception e)
{
Console.WriteLine("Error: Bind failed.");
Console.WriteLine(" {0}", e.Message);
return;
}

// Specify Organizational Unit.
strOU = "OU=TestOU";
strDescription = "ADAM Test Organizational Unit";
Console.WriteLine("Create: {0}", strOU);

// Create Organizational Unit.
try
{
objOU = objADAM.Children.Add(strOU,
"OrganizationalUnit");
objOU.Properties["description"].Add(strDescription);
objOU.CommitChanges();
}
catch (Exception e)
{
Console.WriteLine("Error: Create failed.");
Console.WriteLine(" {0}", e.Message);
return;
}

// Output Organizational Unit attributes.
Console.WriteLine("Success: Create succeeded.");
Console.WriteLine("Name: {0}", objOU.Name);
Console.WriteLine(" {0}",
objOU.Properties["description"].Value);
return;
}
}
}
wqb1979 2005-11-08
  • 打赏
  • 举报
回复
我查了DirectoryEntries.Add的参数,发现第二参数:SchemaClassName有很多,继而查了微软在线的MSDN,发现添加OU的参数是:organizationUnit但还是不行呀
fox7805034 2005-10-27
  • 打赏
  • 举报
回复
我加上一句。在《《c#高级编程》》里有针对ActiveDirectory的讲解。。。需要资料可以看看或google
江东闲客 2005-10-27
  • 打赏
  • 举报
回复
以下是往域里追加用户

private void AddUser(string strDoamin, string strLogin, string strPwd)
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
//obUser.Properties["FullName"].Add("Amigo");
object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
catch (Exception ex)
{
Trace.Warn(ex.Message);
}
}

当然上栏列出的资料也列出了更改密码的方法。
wqb1979 2005-10-27
  • 打赏
  • 举报
回复
加入OU能否也用DirectoryEntries.Add方法?参数怎么写?“OU”?
luojinat2005 2005-10-26
  • 打赏
  • 举报
回复
当然可以
能否说得具体一点
wqb1979 2005-10-26
  • 打赏
  • 举报
回复
to trumf(翦翦风)
你实现了“读”功能,但我要导入ou,就是要涉及到写,如何“写”?
江东闲客 2005-10-26
  • 打赏
  • 举报
回复
还有一些关于DirectoryServices的资料:

DirectoryEntry.Path 属性
获取或设置此 DirectoryEntry 的路径。

Path 属性唯一地标识网络环境中的此项。始终可以使用此 Path 检索此项。

设置 Path 将从目录存储区检索新项;它不更改当前绑定的项的路径。

与 DirectoryEntry 组件关联的类可以与任何 Active Directory 服务提供程序一起使用。当前的一些提供程序包括 Internet 信息服务 (IIS)、轻量目录访问协议 (LDAP)、Novell NetWare 目录服务 (NDS) 和 WinNT。

注意 Path 的一部分,它标识提供程序(在“://”前面),并且区分大小写。例如,“LDAP://”或“WinNT://”。
Path 属性的语法随提供程序不同而不同。一些常见的情况有:

WinNT

连接到计算机上的组。例如“WinNT://<域名>/<计算机名>/<组名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<组名>”。
连接到计算机上的用户。例如“WinNT://<域名>/<计算机名>/<用户名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<用户名>”。
连接到计算机上的服务。例如,“WinNT://<域名>/<计算机名>/<服务名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<服务名>”。
发现网络上的所有域。例如,“WinNT:”。通过枚举此项的子级可以找到这些域。
LDAP

连接到域中的组。例如“LDAP://CN=<组名>, CN =<用户>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
连接到域中的用户。例如“LDAP://CN=<完整用户名>, CN=<用户>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
连接到域中的计算机。例如“LDAP://CN=<计算机名>, CN=<计算机>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
IIS

连接到 Web 目录。例如“IIS://LocalHost/W3SVC/1/ROOT/<Web 目录名>”。
若要使用 LDAP 绑定到当前域,请使用路径“LDAP://RootDSE”,然后获取默认命名上下文,并重新绑定该项。例如:

[C#]
String str = ent.Properties["defaultNamingContext"][0];
DirectoryEntry domain = new DirectoryEntry("LDAP://" + str);

2.-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
WinNT ADsPath

The ADsPath string for the ADSI WinNT provider can be one of the following forms:
"WinNT:"
"WinNT://<domain name>"
"WinNT://<domain name>/<server>"
"WinNT://<domain name>/<path>"
"WinNT://<domain name>/<object name>"
"WinNT://<domain name>/<object name>,<object class>"
The domain name can be either a NETBIOS name or a DNS name.

The server is the name of a specific server within the domain.

The path is the path of on object, such as "printserver1/printer2".

The object name is the name of a specific object.

The object class is the class name of the named object. One example of this usage would be "WinNT://MyDomain/JeffSmith,user". Specifying a class name can improve the performance of the bind operation.
江东闲客 2005-10-26
  • 打赏
  • 举报
回复
刚写个程序把域里所有的机器信息都用TreeView列出来了。
程序如下:
private void button1_Click(object sender, System.EventArgs e)
{
DirectoryEntry entry;
// "WinNT://Domain"
String strPath=@"WinNT://dpsd";
try
{
entry=new DirectoryEntry(strPath);
TreeNode users = new TreeNode("Users");
TreeNode groups = new TreeNode("Groups");
TreeNode services = new TreeNode("Services");
viewPC.Nodes.Clear(); // viewPC is a TreeView Componet
viewPC.Nodes.AddRange(new TreeNode[] { users, groups, services });

foreach(System.DirectoryServices.DirectoryEntry child
in entry.Children)
{
TreeNode newNode = new TreeNode(child.Name);
switch (child.SchemaClassName)
{
case "User" :
users.Nodes.Add(newNode);
break;
case "Group" :
groups.Nodes.Add(newNode);
break;
case "Service" :
services.Nodes.Add(newNode);
break;
}
AddProperties(newNode, child);
}
}
catch (Exception ex)
{
MessageBox.Show( "err:" + ex.Message);
}
}

private void AddProperties(TreeNode node, System.DirectoryServices.DirectoryEntry entry)
{
node.Nodes.Add(new TreeNode("Path: " + entry.Path));
TreeNode propertyNode = new TreeNode("Properties");
node.Nodes.Add(propertyNode);
foreach (string propertyName in entry.Properties.PropertyNames)
{
string oneNode = propertyName + ": " +
entry.Properties[propertyName][0].ToString();
propertyNode.Nodes.Add(new TreeNode(oneNode));
}
}
wqb1979 2005-10-26
  • 打赏
  • 举报
回复
就是怎么用C#向域控制器直接添加OU(组织机构)信息,因为现在在一个MIS系统的数据库里存放了我单位的组织机构信息,有上百个,我怎么一次性导入到域控制器里,关键就是怎么用System.DirectoryServices
xiaomatian 2005-10-26
  • 打赏
  • 举报
回复
这个是肯定可以的.说具体点吧.

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧