企业信息化系统基础——AD:使用C#批量创建帐号
如果一个公司打算使用微软的产品来构建自己的办公自动化系统,那么,建议采用主域控制的方式。那么,必然就要用到活动目录(AD),这样,IT部门就需要为公司的每一个员工来创建域帐号。如果公司比较大的话,这是一个很大的工程。而且,我们会发现,有些工作量基本上是在重复劳动,人力资源部为了给It部门提供人员名单,会录入一次人员的各种信息,比如姓名、工号、所属部门、部门领导、电话号码等等,那么,IT人员在拿到这张表后,他又要重新录入一次。并且常常会因为人为的原因导致帐户中出现错误。下面,我们就用C#编写了一个创建帐户的程序。在这个程序中,它不但要创建域帐户,它还会在相应的Exchange中创建相应的邮件帐户。通过这个程序,人力资源部门只需要按照IT部门提供的数据库格式(Access)填写相关项目就可以了。
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});
首先,我们需要定义一些变量:
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="";
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";
+ "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";
label1.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();
label1.Text="加载数据完毕!开始从数据库中读取新建帐户信息!";
//读取数据库获取帐户信息
ADODB.Connection objConn;
ADODB.Command objCmd;
ADODB.Recordset objRs;
object objOptParm;
objOptParm="";
string str=@"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""db1.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.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=1";
objConn=new ADODB.Connection();
try
{
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();
label1.Text="加载数据完毕!开始从数据库中读取新建帐户信息!";
//读取数据库获取帐户信息
ADODB.Connection objConn;
ADODB.Command objCmd;
ADODB.Recordset objRs;
object objOptParm;
objOptParm="";
string str=@"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""db1.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.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=1";
objConn=new ADODB.Connection();
try
{
objConn.Open(str,"","",-1);
}
catch(SystemException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//
}
objRs=new ADODB.Recordset();
objCmd=new ADODB.Command();
objCmd.CommandText="select * from sheet1";
objCmd.ActiveConnection=objConn;
try
{
objRs=objCmd.Execute(out objOptParm,ref objOptParm,1);
catch(SystemException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//
}
objRs=new ADODB.Recordset();
objCmd=new ADODB.Command();
objCmd.CommandText="select * from sheet1";
objCmd.ActiveConnection=objConn;
try
{
objRs=objCmd.Execute(out objOptParm,ref objOptParm,1);
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
//
}
try
{
//开始创建帐户
//MessageBox.Show(objRs.Fields[2].Value.ToString());
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
container = new DirectoryEntry("LDAP://OU=项目组,OU=部门," + defaultNC);
//读取数据
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
//
}
try
{
//开始创建帐户
//MessageBox.Show(objRs.Fields[2].Value.ToString());
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
container = new DirectoryEntry("LDAP://OU=项目组,OU=部门," + defaultNC);
//读取数据
while (!objRs.EOF)
{
//读取数据
fullName=objRs.Fields[1].Value.ToString();
alias=objRs.Fields[4].Value.ToString();
strGivenName=objRs.Fields[2].Value.ToString();
{
//读取数据
fullName=objRs.Fields[1].Value.ToString();
alias=objRs.Fields[4].Value.ToString();
strGivenName=objRs.Fields[2].Value.ToString();
label1.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"检查有无重复帐号!";
//检查是否有重复的帐号
DirectoryEntry su=new DirectoryEntry("LDAP://DC=Test,DC=net");
//检查是否有重复的帐号
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
// {
//创建帐户
label1.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");
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
// {
//创建帐户
label1.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(2004,10,31,0,0,0,0);
long longAE=dt.Ticks;
longAE=longAE-504910656000000000;//减去8个时区
user.Properties["accountExpires"].Add(longAE.ToString());//帐号到期时间
DateTime dt=new DateTime(2004,10,31,0,0,0,0);
long longAE=dt.Ticks;
longAE=longAE-504910656000000000;//减去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 = 0x200; //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());
}
user.Invoke("SetPassword", new object[]{password});
user.CommitChanges();
//This enables the new user.
user.Properties["userAccountControl"].Value = 0x200; //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());
}
// }
label1.Text="创建帐户:"+fullName+"-"+alias+"-"+strGivenName+"创建完毕!";
objRs.MoveNext();
}
objRs.MoveNext();
}
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
objRs.Close();
objConn.Close();
MessageBox.Show("ok");
}
}
finally
{
objRs.Close();
objConn.Close();
MessageBox.Show("ok");
}
}