AD:使用C#批量创建帐号
首先,我們需要定義一些變量:
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";
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
{
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)
{
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();
label1.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
// {
//創建帳戶
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());//帳號到期時間
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());
}
// }
label1.Text="創建帳戶:"+fullName+"-"+alias+"-"+strGivenName+"創建完畢!";
objRs.MoveNext();
}
}
catch(SystemException ex)
{
objConn.Close();
MessageBox.Show(ex.Message);
}
finally
{
objRs.Close();
objConn.Close();
MessageBox.Show("ok");
}
}
轉載于:https://www.cnblogs.com/ahjxxy/archive/2009/10/23/1588769.html
總結
以上是生活随笔為你收集整理的AD:使用C#批量创建帐号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我从万花筒里看成功
- 下一篇: C#中的DBNull、Null、和Str