If you want to take the stat while processing the entries of Active Directory then
it is must that Active Directory should have more entries so that you can process
those entries. But creating the bulk entries (like 10000 users) is not an easy
task and also we cannot create entry manually. For this there should be any automated
process which will import the entries in Active Directory.
For bulk import I have written this c# code which will create bulk entries in Active
directory. Using this code you can insert Organizational Units, Groups, and Users.
Also you can add the users to groups as a member.
First Add the below reference in your application
System.DirectoryServices
After adding the reference, add the below namespace to your code.
using System.DirectoryServices;
Code Implementation-
private void btnBulkImport_Click(object sender, EventArgs e)
{
string strPostPath = "DC=yourdomain,DC=com";
//Name of Parent OU where child OU will be created
string strMainOU = "TestOU";
//Path to main OU
string strPath = "LDAP://OU=TestOU,DC=yourdomain,DC=com";
//Set the values to import bulk import
int intNoOfOUToCreate = 2;
int intNoOfGroupsForEachOU = 2;
int intNoOfUsersForEachOU = 5000;
int intNoOfMembersForEachGroup = 50;
try
{
//Adding OU to Main OU
for (int indexOU = 1; indexOU <= intNoOfOUToCreate; indexOU++)
{
strPath = "LDAP://OU=TestOU,DC=yourdomain,DC=com";
DirectoryEntry objDEOU = new DirectoryEntry(strPath);
//OU Name
string strOUName = "OU" + indexOU.ToString();
DirectoryEntry objOU = objDEOU.Children.Add("OU=" + strOUName, "organizationalUnit");
objOU.CommitChanges();
objDEOU.CommitChanges();
ArrayList objUserList = new ArrayList();
//Adding Users to recently created OU
for (int indexUser = 1; indexUser <= intNoOfUsersForEachOU; indexUser++)
{
strPath = "LDAP://OU=" + strOUName + ",OU=" + strMainOU + "," + strPostPath;
DirectoryEntry objDEUser = new DirectoryEntry(strPath);
//User Name
string strUserName = "User" + indexUser.ToString() + "_" + strOUName;
DirectoryEntry objUser = objDEUser.Children.Add("CN=" + strUserName, "user");
// User name (domain based)
objUser.Properties["userprincipalname"].Add(strUserName + "@yourdomain.com" );
// User name (older systems)
objUser.Properties["samaccountname"].Add(strUserName);
// Surname
objUser.Properties["sn"].Add(strUserName + "_LastName");
// Forename
objUser.Properties["givenname"].Add(strUserName);
// Display name
objUser.Properties["displayname"].Add(strUserName + " " + strUserName + "_LastName");
// Description
objUser.Properties["description"].Add(strUserName + "_desc");
// E-mail
objUser.Properties["mail"].Add(strUserName + "@yourdomain.com");
objUser.CommitChanges();
objDEUser.CommitChanges();
//Password
objUser.Invoke("SetPassword", new object[] { "second@123" });
objUser.CommitChanges();
//Adding created user to list
objUserList.Add(strUserName);
}
//Adding Groups to recently created OU
for (int indexGroup = 1; indexGroup <= intNoOfGroupsForEachOU; indexGroup++)
{
strPath = "LDAP://OU=" + strOUName + ",OU=" + strMainOU + "," + strPostPath;
DirectoryEntry objDEGroup = new DirectoryEntry(strPath);
// Group Name
string strGroupName = "Group" + indexGroup.ToString() + "_" + strOUName;
DirectoryEntry group = objDEGroup.Children.Add("CN=" + strGroupName, "group");
// Description
group.Properties["description"].Add(strGroupName + "_desc");
group.CommitChanges();
objDEGroup.CommitChanges();
//Adding user to the recenlty created group as member
strPath = "LDAP://" + "yourdomain.com/" + "CN=" + strGroupName + ",OU=" + strOUName + ",OU=" + strMainOU + "," + strPostPath;
//Adding members to Group
ArrayList objGroupUserList = new ArrayList();
for (int indexUser = 1; indexUser <= intNoOfMembersForEachGroup; indexUser++)
{
//Getting random user from List
Random rnd = new Random();
int randomUser = rnd.Next(1, intNoOfUsersForEachOU);
string strGroupUser = Convert.ToString(objUserList[randomUser]);
//If user is already exist in group then skip that user
if (objGroupUserList.Contains(strGroupUser))
{
continue;
}
string ustr = "CN=" + strGroupUser + ",OU=" + strOUName + ",OU=" + strMainOU + "," + strPostPath;
DirectoryEntry ent = new DirectoryEntry(strPath);
ent.Properties["member"].Add(ustr);
ent.CommitChanges();
objGroupUserList.Add(strGroupUser);
}
}
}
MessageBox.Show("Process Completed...");
}
catch (Exception ex)
{
MessageBox.Show (ex.Message ) ;
}
}
Description-
In above code I have taken some variables-
intNoOfOUToCreate - Number of Organization Units to create in Active
Directory
intNoOfGroupsForEachOU - Number of Groups to create in each OU
intNoOfUsersForEachOU - Number of Users to create in each OU
intNoOfMembersForEachGroup - Number of Users to add as a member in each
Group
Based on given values for these variables for loop will run and it will create the
Active Directory structure.
You can download the code from here- Active Directory Bulk Import