From time to time, it might be required that large number of generic user account objects be created in an Active Directory domain. Here is how to do it, quickly.
Why Would You Want 5000 Generic Users?
Admittedly, this doesn’t happen very often. But let’s say that your testing a new automated process, or maybe doing some performance testing against an application. You’d want to generate a large number of unique user account objects for this.
Rather than using Active Directory Users and Computers, you can just script it with VBScript. The code is below; it’s short and pretty easy to understand.
' *** get the domain name, and set the container (OU) that the
' *** new users are to be created in
set objRootDsE=Getobject ("LDAP ://rootDsE")
set objcontainer=Getobject("LDAP://cn=users," & objRootDsE.Get("DefaultNamingContext"))
For Kounter=1 to 5000
' *** format the number into a four character string
strUserNumber=formatNumber(Kounter,0,0,0)
strUserNumber=Trim(strUserNumber)
If len(struserNumber)<4 then struserNumber=string(4-Len(struserNumber) ,"0") & struserNumber
strUserNumber=Replace(strUserNumber,",","")
' *** create the user
set objLeaf=objcontainer.create("user", "cn=userno" & struserNumber)
objLeaf.setinfo
Next
' *** inform the user
wscript.echo "completed!"
What’s happening
- First, the name of the current domain is retrieved via an LDAP query.
- Next, we set up the container where the new users are going to reside. You can change this easily by modifying the LDAP string specified.
- Now, a loop is started, with 5000 being the top end. Pretty straightforward. Inside this loop, two things are accomplished.
- Some basic string handling, which gives us a uniform length string representing the user sequence number. This is basically a zero-padding function, with a prefix of “userno”. After that, the user is created in the container.
- Finally, the loop is concluded, and the user is notified.
Not a terribly complicated script, but I have been asked for something like this at least twice, so I figured there might be someone else out there in that might find it useful.
If you have any questions or comments, please feel free to drop a message below in the comments section, or you can post a message in the forums.