.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
使用C#批量修改域帐户信息

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

 公司的组织结构经常发生变化,而我们的域帐户信息(AD)是和真实的组织机构相对应的。组织机构变化了,我们自然要改动相应的域帐户信息啦。这是一件很痛苦的事情,原因是什么,大家都明白。那么能不能用程序来解决这个问题呢?(windows2003的管理工具好像已经支持批量修改域帐户信息了)。

我创建了一个windows应用程序来解决:

Form上放置了六个comboBox用于选择公司的OU(可以选择五级OU,如果你喜欢可以用TreeView更好些)。

加载Form的时候初始化这些comboBox

private void Form1_Load(object sender, System.EventArgs e)

{

//初始化公司选择框

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://DC=test,DC=net";

try

{

//所有OU(第一级)

foreach (DirectoryEntry ch1 in de1.Children)

{

str=ch1.Name.ToString();

string str1="";

//str1=str.Substring(0,str.IndexOf("="));

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

//listBox1.Items.Add(ch1.Name.ToString());

//加入第一个combobox

comboBox1.Items.Add(ch1.Name.ToString());

//

comboBox3.Items.Add(ch1.Name.ToString());

}

}

 

de1.Close();

//textBox1.Text=textBox1.Text+"--------------next------------------------\r\n";

//

MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

 

strErr=ex.Message;

 

}

finally

{}

 

 

}

 

在初始化form的时候在第一个combobox中显示出所有的第一层OU。然后,在选择了这个combobox的时候,在第二个combobox中显示下一级OU

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

//str=listBox1.SelectedItem.ToString();

str=comboBox1.SelectedItem.ToString();

DirectoryEntry de1=new DirectoryEntry();

de1.Path="LDAP://"+str+",DC=test,DC=net";

try

{

comboBox2.Items.Clear();

comboBox2.Text="";

comboBox2.Refresh();

foreach (DirectoryEntry ch1 in de1.Children)

{

//

textBox1.Text=textBox1.Text+str+"\r\n";//ch.Properties["adpath"][0].ToString();

string str1="";

str1=ch1.SchemaClassName.ToString();

if (str1=="organizationalUnit")

{

comboBox2.Items.Add(ch1.Name.ToString());

}

}

 

de1.Close();

 

//textBox1.Text=textBox1.Text+"--------------next------------------------\r\n";

//

MessageBox.Show("finish!!!");

}

catch(Exception ex)

{

 

MessageBox.Show(ex.Message);

}

finally

{}

 

}

 

依次在下一个combobox中显示下一级OU。选择好要修改的OU下后,就可以修改该ou下所有的信息了。这里以修改部门名称为例。

使用一个Textbox输入部门名称,放一个按钮触发修改动作:

private void button1_Click(object sender, System.EventArgs e)

{

string strADRoot="";

string strName="";

//str中保存的是OUADPath的一部分,即通过选择combobox产生的字符串,类似于ou=imd,OU=company

 

strADRoot="LDAP://"+str+",DC=test,DC=net";

DirectoryEntry de=new DirectoryEntry();

de.Path=strADRoot;

//修改所有的user对象

foreach(DirectoryEntry chm in de.Children)

{

string strType="";

strType=chm.SchemaClassName.ToString();

if(strType.ToUpper()=="USER")

{

strName=chm.Name.ToString();

//判断是否属性值是否为空

if(chm.Properties.Contains("department"))

{

chm.Properties["department"][0]=textBox2.Text.ToString();

chm.CommitChanges();

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称修改成功!\r\n";

}

else

{

chm.Properties["department"].Add(textBox2.Text.ToString());

chm.CommitChanges();

//textBox3.Text=textBox3.Text+ch1.Name .ToString()+"\r\n";

textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称添加成功!\r\n";

}

}

 

 

 

}

 

 

 




相关文章

相关软件