如何用C#编程方式批量对域控制器添加OU(http://topic.csdn.net/t/20051026/11/4351176.html)
【求助】如何用C#编程方式批量对域控制器添加OU
现在单位的部门(包括子部门)有上百个,之前做了一个管理系统已经有把
不够分可以再加。
问题点数:100、回复次数:11Top
1 楼luojinat2005()回复于 2005-10-26 12:33:19 得分 0
当然可以
能否说得具体一点
Top
2 楼xiaomatian(趴趴熊◎%#……※×)回复于 2005-10-26 13:38:30 得分 0
这个是肯定可以的.说具体点吧.Top
3 楼wqb1979(临风)回复于 2005-10-26 16:56:44 得分 0
就是怎么用C#向域
Top
4 楼trumf(翦翦风)回复于 2005-10-26 17:27:53 得分 0
刚写个程序把域里所有的机器信息都用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));
}
}Top
5 楼trumf(翦翦风)回复于 2005-10-26 17:30:51 得分 0
还有一些关于DirectoryServices的资料:
DirectoryEntry.Path 属性
获取或设置此 DirectoryEntry 的路径。
Path 属性唯一地标识
设置 Path 将从目录存储区检索新项;它不更改当前绑定的项的路径。
与 DirectoryEntry
注意 Path 的一部分,它标识提供程序(在“://”前面),并且区分大小写。例如,“LDAP://”或“WinNT://”。
Path 属性的
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.
Top
6 楼wqb1979(临风)回复于 2005-10-26 17:46:15 得分 0
to trumf(翦翦风)
你实现了“读”功能,但我要导入ou,就是要涉及到写,如何“写”?Top
7 楼trumf(翦翦风)回复于 2005-10-27 09:00:21 得分 15
以下是往域里追加用户
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);
}
}
当然上栏列出的资料也列出了更改密码的
8 楼fox7805034(肚子饿了)回复于 2005-10-27 09:12:38 得分 0
我加上一句。在《《c#高级编程》》里有针对ActiveDirectory的讲解。。。需要资料可以看看或googleTop
9 楼wqb1979(临风)回复于 2005-10-27 15:54:44 得分 0
加入OU能否也用DirectoryEntries.Add方法?参数怎么写?“OU”?Top
10 楼wqb1979(临风)回复于 2005-11-08 22:43:32 得分 0
我查了DirectoryEntries.Add的参数,发现第二参数:SchemaClassName有很多,继而查了微软在线的MSDN,发现添加OU的参数是:organizationUnit但还是不行呀Top
11 楼spgoal(敏捷的狗狗)回复于 2005-11-09 21:37:47 得分 85
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adam/adam/creating_organizational_units.aspThe 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;
}
}
}