转 :AD:使用C#批量创建帐号 http://www.itepub.net
2008-01-09 11:41
716 查看
如果一个公司打算使用微软的产品来构建自己的办公自动化系统,那么,建议采用主域控制的方式。那么,必然就要用到活动目录(AD),这样,IT部门就需要为公司的每一个员工来创建域帐号。如果公司比较大的话,这是一个很大的工程。而且,我们会发现,有些工作量基本上是在重复劳动,人力资源部为了给It部门提供人员名单,会录入一次人员的各种信息,比如姓名、工号、所属部门、部门领导、电话号码等等,那么,IT人员在拿到这张表后,他又要重新录入一次。并牵常会因为人为的原因导致帐户中出现错误。下面,我们就用C#编写了一个创建帐户的程序。在这个程序中,它不但要创建域帐户,它还会在相应的Exchange中创建相应的邮件帐户。通过这个程序,人力资源部门只需要按照IT部门提供的数据库格式(Access)填写相关项目就可以了。
首先,我们需要定义一些变量:
string strMemberof="";
string strUserParm="";
string strManager="";
string strScriptPath="";
string strdepartment="";
string strCompany="";
// string strAccountExp;
string defaultNC = "DC=Test,DC=net"; //这是默认的域
string alias = "";
string fullName = "";
string password = @"PassWord"; //这是默认的初始密码
string domainName = "test.net";
string strGivenName="";
//下面这个变量告诉程序将邮箱建在Exchange的哪个存储区域中
string homeMDB = "CN=Test,CN=控股公司,"
+ "CN=InformationStore,CN=MAIL,CN=Servers,"
+ "CN=First Administrative Group,CN=Administrative Groups,"
+ "CN=test,CN=Microsoft Exchange,CN=Services,"
+ "CN=Configuration,DC=Test,DC=net";
label.Text="开始从模板中加载数据!";
//获取模板信息
我们知道,创建的一批帐户中,有许多的项目是相同的,所以,我们先创建好一个帐户作为模板,然后,通过读取这个模板的数据作为新建的帐户的相应项目的数据。
这段代码采用了Ad的查询对象:
DirectoryEntry deMb = new DirectoryEntry();
deMb.Path="LDAP://CN=模板, OU=项目组,OU=部门,DC=Test, DC=net";
strMemberof=deMb.Properties["memberof"][0].ToString();
strUserParm=deMb.Properties["UserParameters"][0].ToString();
strManager=deMb.Properties["manager"][0].ToString();
strScriptPath=deMb.Properties["scriptPath"][0].ToString();
strdepartment=deMb.Properties["department"][0].ToString();
strCompany=deMb.Properties["company"][0].ToString();
// strAccountExp=deMb.Properties["accountExpires"].Value.ToString();
deMb.Close();
label.Text="加载数据完毕!开始从数据库中读取新建帐户信息!";
//读取数据库获取帐户信息
ADODB.Connection objConn;
ADODB.Command objCmd;
ADODB.Recordset objRs;
object objOptParm;
objOptParm="";
string str=@"Jet OLEDB:Global Partial Bulk Ops=;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=;Data Source=""db.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=;Provider=""Microsoft.Jet.OLEDB..0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=";
objConn=new ADODB.Connection();
try
{
objConn.Open(str,"","",-);
}
catch(SystemException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//
}
objRs=new ADODB.Recordset();
objCmd=new ADODB.Command();
objCmd.CommandText="select * from sheet";
objCmd.ActiveConnection=objConn;
try
{
objRs=objCmd.Execute(out objOptParm,ref objOptParm,);
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
//
}
try
{
//开始创建帐户
//MessageBox.Show(objRs.Fields[].Value.ToString());
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
container = new DirectoryEntry("LDAP://OU=项目组,OU=部门," + defaultNC);
//读取数据
while (!objRs.EOF)
{
//读取数据
fullName=objRs.Fields[].Value.ToString();
alias=objRs.Fields[].Value.ToString();
strGivenName=objRs.Fields[].Value.ToString();
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"检查有无重复帐号!";
//检查是否有重复的帐号
DirectoryEntry su=new DirectoryEntry("LDAP://DC=Test,DC=net");
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot=su;
searcher.Filter = "(&(objectClass=user)(sAMAccountName="+alias+"))";
searcher.SearchScope = SearchScope.Subtree;
searcher.Sort = new SortOption("givenName", SortDirection.Ascending);
SearchResultCollection results = searcher.FindAll();
if(results.Count>0)
{
//表明有重复的帐号,修改fullname和alias
fullName=fullName+strGivenName;
alias=alias+strGivenName;
}
// else
// {
//创建帐户
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName;
try
{
user = container.Children.Add("cn=" + fullName, "user");
user.Properties["sAMAccountName"].Add(alias);//帐户
user.Properties["userPrincipalName"].Add((alias+"@Test.net"));
user.Properties["givenName"].Add(strGivenName);//工号
user.Properties["sn"].Add(fullName);//姓
// user.Properties["telephoneNumber"].Add("0000");//电话
// user.Properties["mobile"].Add("00000000000");//手机
user.Properties["company"].Add(strCompany);//公司
user.Properties["department"].Add(strdepartment);//部门
// user.Properties["physicalDeliveryOfficeName"].Add("0000");
//这里要说明一下:这里是要设置帐户的到期时间,因为,根据我们的规定,如果在帐户到期之前,没有通过考试的话,那么帐户就会禁用。可是,AD中这个字段是整形的,我不知道怎么去换算它,所以就有以下这段代码,希望,有高手可以指点一下。
DateTime dt=new DateTime(00,0,,0,0,0,0);
long longAE=dt.Ticks;
longAE=longAE-09066000000000;//减去8个时区
user.Properties["accountExpires"].Add(longAE.ToString());//帐号到期时间
user.Properties["msNPAllowDialin"].Value=false;//禁止拨入
user.Properties["userParameters"].Add(strUserParm);//禁止终端服务
user.Properties["scriptPath"].Add(strScriptPath);//配置文件
user.Properties["manager"].Add(strManager);//领导
user.Properties["userPassword"].Add(password);
// user.Invoke("SetPassword", new object[]{password});
user.CommitChanges();
user.Invoke("SetPassword", new object[]{password});
user.CommitChanges();
//This enables the new user.
user.Properties["userAccountControl"].Value = 0x00; //ADS_UF_NORMAL_ACCOUNT
user.CommitChanges();
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
// }
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"创建完毕!";
objRs.MoveNext();
}
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
objRs.Close();
objConn.Close();
MessageBox.Show("ok");
}
}
首先,我们需要定义一些变量:
string strMemberof="";
string strUserParm="";
string strManager="";
string strScriptPath="";
string strdepartment="";
string strCompany="";
// string strAccountExp;
string defaultNC = "DC=Test,DC=net"; //这是默认的域
string alias = "";
string fullName = "";
string password = @"PassWord"; //这是默认的初始密码
string domainName = "test.net";
string strGivenName="";
//下面这个变量告诉程序将邮箱建在Exchange的哪个存储区域中
string homeMDB = "CN=Test,CN=控股公司,"
+ "CN=InformationStore,CN=MAIL,CN=Servers,"
+ "CN=First Administrative Group,CN=Administrative Groups,"
+ "CN=test,CN=Microsoft Exchange,CN=Services,"
+ "CN=Configuration,DC=Test,DC=net";
label.Text="开始从模板中加载数据!";
//获取模板信息
我们知道,创建的一批帐户中,有许多的项目是相同的,所以,我们先创建好一个帐户作为模板,然后,通过读取这个模板的数据作为新建的帐户的相应项目的数据。
这段代码采用了Ad的查询对象:
DirectoryEntry deMb = new DirectoryEntry();
deMb.Path="LDAP://CN=模板, OU=项目组,OU=部门,DC=Test, DC=net";
strMemberof=deMb.Properties["memberof"][0].ToString();
strUserParm=deMb.Properties["UserParameters"][0].ToString();
strManager=deMb.Properties["manager"][0].ToString();
strScriptPath=deMb.Properties["scriptPath"][0].ToString();
strdepartment=deMb.Properties["department"][0].ToString();
strCompany=deMb.Properties["company"][0].ToString();
// strAccountExp=deMb.Properties["accountExpires"].Value.ToString();
deMb.Close();
label.Text="加载数据完毕!开始从数据库中读取新建帐户信息!";
//读取数据库获取帐户信息
ADODB.Connection objConn;
ADODB.Command objCmd;
ADODB.Recordset objRs;
object objOptParm;
objOptParm="";
string str=@"Jet OLEDB:Global Partial Bulk Ops=;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=;Data Source=""db.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=;Provider=""Microsoft.Jet.OLEDB..0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=";
objConn=new ADODB.Connection();
try
{
objConn.Open(str,"","",-);
}
catch(SystemException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//
}
objRs=new ADODB.Recordset();
objCmd=new ADODB.Command();
objCmd.CommandText="select * from sheet";
objCmd.ActiveConnection=objConn;
try
{
objRs=objCmd.Execute(out objOptParm,ref objOptParm,);
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
//
}
try
{
//开始创建帐户
//MessageBox.Show(objRs.Fields[].Value.ToString());
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
container = new DirectoryEntry("LDAP://OU=项目组,OU=部门," + defaultNC);
//读取数据
while (!objRs.EOF)
{
//读取数据
fullName=objRs.Fields[].Value.ToString();
alias=objRs.Fields[].Value.ToString();
strGivenName=objRs.Fields[].Value.ToString();
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"检查有无重复帐号!";
//检查是否有重复的帐号
DirectoryEntry su=new DirectoryEntry("LDAP://DC=Test,DC=net");
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot=su;
searcher.Filter = "(&(objectClass=user)(sAMAccountName="+alias+"))";
searcher.SearchScope = SearchScope.Subtree;
searcher.Sort = new SortOption("givenName", SortDirection.Ascending);
SearchResultCollection results = searcher.FindAll();
if(results.Count>0)
{
//表明有重复的帐号,修改fullname和alias
fullName=fullName+strGivenName;
alias=alias+strGivenName;
}
// else
// {
//创建帐户
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName;
try
{
user = container.Children.Add("cn=" + fullName, "user");
user.Properties["sAMAccountName"].Add(alias);//帐户
user.Properties["userPrincipalName"].Add((alias+"@Test.net"));
user.Properties["givenName"].Add(strGivenName);//工号
user.Properties["sn"].Add(fullName);//姓
// user.Properties["telephoneNumber"].Add("0000");//电话
// user.Properties["mobile"].Add("00000000000");//手机
user.Properties["company"].Add(strCompany);//公司
user.Properties["department"].Add(strdepartment);//部门
// user.Properties["physicalDeliveryOfficeName"].Add("0000");
//这里要说明一下:这里是要设置帐户的到期时间,因为,根据我们的规定,如果在帐户到期之前,没有通过考试的话,那么帐户就会禁用。可是,AD中这个字段是整形的,我不知道怎么去换算它,所以就有以下这段代码,希望,有高手可以指点一下。
DateTime dt=new DateTime(00,0,,0,0,0,0);
long longAE=dt.Ticks;
longAE=longAE-09066000000000;//减去8个时区
user.Properties["accountExpires"].Add(longAE.ToString());//帐号到期时间
user.Properties["msNPAllowDialin"].Value=false;//禁止拨入
user.Properties["userParameters"].Add(strUserParm);//禁止终端服务
user.Properties["scriptPath"].Add(strScriptPath);//配置文件
user.Properties["manager"].Add(strManager);//领导
user.Properties["userPassword"].Add(password);
// user.Invoke("SetPassword", new object[]{password});
user.CommitChanges();
user.Invoke("SetPassword", new object[]{password});
user.CommitChanges();
//This enables the new user.
user.Properties["userAccountControl"].Value = 0x00; //ADS_UF_NORMAL_ACCOUNT
user.CommitChanges();
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
// }
label.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"创建完毕!";
objRs.MoveNext();
}
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
objRs.Close();
objConn.Close();
MessageBox.Show("ok");
}
}
相关文章推荐
- 企业信息化系统基础——AD:使用C#批量创建帐号
- 企业信息化系统基础——AD:使用C#批量创建帐号
- 企业信息化系统基础——AD:使用C#批量创建帐号
- 企业信息化系统基础——AD:使用C#批量创建帐号
- AD:使用C#批量创建帐号
- 企业信息化系统基础——AD:使用C#批量创建帐号
- 企业信息化系统基础——AD:使用C#批量创建帐号
- Window 消息大全使用详解http://www.itepub.net/html/kaifajingcui/C___VC/2006/0515/38024.html
- 使用ADS 创建工程 http://www.softwarehistory.net/h/ARM_KaifaXiangjie/44.php
- SQL Server使用Bulk Insert把一个文本导入到数据库--http://www.wangchao.net.cn/it/detail_59013.html
- 使用c#在vs2005下写的换桌面图片的小程序(from: http://www.wangchao.net.cn/bbsdetail_26766.html)
- 使用C#批量创建帐号
- 如何使用 Visual C# .NET 创建 ASP.NET HTTP 模块
- 使用C#批量创建帐号
- 在T-SQL中创建和使用表参数值(TVP)(转自:http://www.ixpub.net/thread-2751953-1-1.html)
- 使用命令行 Subversion 访问项目源文件(转自:http://www.open.collab.net/nonav/scdocs/ddUsingSVN_command-line.html.zh)
- .net Core使用Orcle官方驱动连接数据库 C#参考教程 http://www.csref.cn
- ASP.NET 缓存(四)--使用缓存键依赖项缓存页输出http://www.opent.cn/?action=show&id=191
- 浅析ASP.NET的Page.IsPostBack 属性http://www.sina.com.cn 2008年05月06日 11:33 IT168.com
- 创建安全的ASP.NET虚拟主机 http://www.xfoksite.net/Support/7/2005/01/24/56.htm