February 2009 - Posts

Dynamically create domain users
Tuesday, February 10, 2009 2:14 AM

My Team was planning a Stress test scenario to be run on our stress lab, the application we were planning to test needed windows authentication to function.

The plan was to create a set of users with predefined passwords and add them to the domain so the application can be tested correctly. We used VBscript that creates the users from a database table this is the code used.

Dim aConnectionString
aConnectionString = "Provider=SQLOLEDB;Data " _
& "Source=srvr;Database=UrDataBaseName;" _
& "UID=usr; PWD=psw;"
Dim conn,R,SQL,RecsAffected
'connect to database
Set conn=CreateObject("ADODB.Connection")
conn.Mode =3
conn.ConnectionString = aConnectionString
conn.Open
Dim strCommand
Dim Users,comm,UsersCommandText
Set comm=CreateObject("ADODB.Command")
comm.ActiveConnection =conn
UsersCommandText= "SELECT [UserName] from [Users]"
comm.commandtext= UsersCommandText
Set Users = Comm.Execute
' this is like assigning 
' a reader with the result set
' only here it's called a recordset
If Not (Users.Eof and Users.Bof ) Then
Users.MoveNext
While Not Users.EOF
  
  Dim strUser
  Dim objRootLDAP, objContainer, objNewUser
  strUser = Users("UserName")
  
  ' Bind to Active Directory, Users container.
  Set objRootLDAP = GetObject("LDAP://YourDomainName")
  Dim defaultContext  
  defaultContext = "DC=YourDomainName,DC=com" 
  Set objContainer = GetObject("LDAP://cn=Users," & defaultContext  ) 
  ' now this is the container that you are going to use
  
  ' now you can Build the actual User.
  Set objNewUser = objContainer.Create("User", "cn=" & strUser)
  objNewUser.Put "sAMAccountName", strUser
  objNewUser.AccountDisabled=False  
  objNewUser.setpassword "123"
        objNewUser.SetInfo 
  
  Set objNewUser = objContainer.GetObject("User", "cn=" & strUser)
  objNewUser.AccountDisabled=False
  objNewUser.SetInfo   
  
  MsgBox (Users("UserName")) 
  ' seriously don't do this if u have a few hundred users
  Users.MoveNext
Wend
End If
conn.Close
by amir.magdy | 1 comment(s)
Filed under: ,