Foundation of Enterprise Informatization system--ad: Using C # to create accounts in bulk

Source: Internet
Author: User
Tags compact datetime time zones ldap mail tostring mail account samaccountname
Create if a company intends to use Microsoft's products to build its own office automation system, then it is recommended to use the primary domain control approach. Then, the Active Directory (AD) must be used, so that the IT department needs to create a domain account for each employee of the company. If the company is big, this is a very big project. And, we will find that some of the workload is basically duplication of labor, the Human resources department in order to give IT departments to provide a list of personnel, will input a variety of information, such as name, work number, department, department leader, telephone number, etc., then the IT staff in the form, he has to re-enter again. And often causes errors in the account for human reasons. Below, we wrote a program to create an account in C #. In this program, not only does it create a domain account, it also creates a corresponding mail account in the appropriate exchange. With this program, the HR department only needs to fill in the relevant projects in the database format (Access) provided by the IT department.
First, we need to define some variables:
String strmemberof= "";
String Struserparm= "";
String Strmanager= "";
String Strscriptpath= "";
String Strdepartment= "";
String Strcompany= "";
String Straccountexp;
String DEFAULTNC = "Dc=test,dc=net"; This is the default domain
String alias = "";
String fullName = "";
string password = @ "password"; This is the default initial password
string domainname = "Test.net";
String Strgivenname= "";

The following variable tells the program where to build the mailbox in which storage area of exchange
String HomeMDB = "cn=test,cn= Holding company,"
+ "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= "Start loading data from the template!" ";
Get template information

We know that many of the items created in the account are the same, so we first create an account as a template, and then, by reading the template's data as the corresponding item for the new account.
This code uses the query object for AD:
DirectoryEntry DEMB = new DirectoryEntry ();
Demb.path= "ldap://cn= template, ou= project team, ou= Department, 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= "Load data complete!" Start reading new account information from the database! ";
Read database get account information
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
{

Start creating an Account
MessageBox.Show (Objrs.fields[2]. Value.tostring ());
DirectoryEntry container, user;
CDOEXM. IMailboxStore mailbox;
container = new DirectoryEntry ("Ldap://ou= project team, ou= Department," + DEFAULTNC);
Reading data
while (!objrs.eof)
{
Reading data
FULLNAME=OBJRS.FIELDS[1]. Value.tostring ();
ALIAS=OBJRS.FIELDS[4]. Value.tostring ();
STRGIVENNAME=OBJRS.FIELDS[2]. Value.tostring ();
Label1. Text= "Create an account:" +fullname+ "-" +alias+ "-" +strgivenname+ "Check for duplicate account number! ";
Check to see if there are duplicate accounts
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)
{
Indicate a duplicate account number, modify FullName and Alias
Fullname=fullname+strgivenname;
Alias=alias+strgivenname;

}
Else
// {
Create an Account
Label1. Text= "Create an account:" +fullname+ "-" +alias+ "-" +strgivenname;
Try
{

user = container. Children.add ("cn=" + fullName, "user");
User. properties["sAMAccountName"]. Add (alias);//Account
User. properties["userPrincipalName"]. ADD ((alias+ "@Test. NET"));
User. properties["givenname"]. ADD (strgivenname);//Work No.
User. properties["SN"]. ADD (fullName);//Last Name
User. properties["Telephonenumber"]. ADD ("0000");//Telephone
User. properties["mobile". ADD ("00000000000");//Mobile phone
User. Properties["Company"]. ADD (Strcompany);//Company
User. properties["department"]. ADD (strdepartment);/department
User. properties["physicalDeliveryOfficeName"]. ADD ("0000");

Here is to explain: this is to set the account expiration time, because, according to our rules, if the account expires before the test, then the account will be disabled. However, the ad in this field is plastic, I do not know how to convert it, so there is the following code, hope, have a master can be pointing.
DateTime dt=new datetime (2004,10,31,0,0,0,0);
Long Longae=dt. Ticks;
longae=longae-504910656000000000;//minus 8 time zones
User. properties["Accountexpires"]. ADD (Longae.tostring ());//Account Expiration time


User. properties["msNPAllowDialin"]. value=false;//Prohibit dial-in
User. properties["Userparameters"]. ADD (struserparm);/Disable Terminal Services
User. properties["ScriptPath"]. ADD (Strscriptpath);//configuration file
User. properties["Manager". ADD (Strmanager);/leader
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= "Create account: +fullname+"-"+alias+"-"+strgivenname+" created! ";
Objrs.movenext ();

}

}
catch (SystemException ex)
{
Objconn.close ();
MessageBox.Show (ex. message);
}
Finally
{
Objrs.close ();
Objconn.close ();
MessageBox.Show ("OK");
}


}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.