Putting It All Together
In the previous chapter, I demonstrated a script that sets up a domain with some OUs, designed to model a production environment in a test lab. But what's a domain without users?
Preload Domain II
Listing 16.2 shows a script that utilizes everything I've covered in this chapter. It's designed to be added to the end of Listing 15.1 for a complete domain preloading script. This script creates a couple of thousand users accounts, some groups, and distributes users into the groups.
Listing 16.2. PreloadDomain2.vbs. Creating dummy user and group accounts for a domain in a test environment.
'create 10,000 user accounts
'seriously - don't run this in a
'production domain!
'connect to the root
Dim oRoot
Set oRoot = GetObject("LDAP://rootDES")
'connect to the Users container
Dim oContainer
Set oContainer = GetObject("LDAP://cn=Users," & _
oRoot.Get("defaultNamingContext")
'create 10,000 users
Dim iUser, oUser
For iUser = 1 To 10000
Set oUser = oContainer.Create("user", _
"DummyUser" & CStr(iUser)
oUser.SetInfo
Next
'create 1,000 groups
Dim iGroup, oGroup
For iGroup = 1 To 1000
Set oGroup = oContainer.Create("group", _
"DummyGroup" & CStr(iGroup)
oGroup.SetInfo
Next
'go through the users and place
'1,000 of them in each group
Dim iLastUser
iLastUser = 1
For iGroup = 1 To 1000
'get the group
Set oGroup = GetObject("LDAP://cn=DummyGroup" & _
CStr(iGroup) & ",dc=domain,dc=com")
'go through users
For iUser = iLastUser To iLastUser + 999
oGroup.PutEx 3, "member", _
"cn=DummyUser" & CStr(iUser) & _
",dc=domain,dc=com"
Next
iLastUser = iUser
Next
Please, please, please note: Don't run this in a production domain. It's intended only for use in a test lab, and it will create 10,000 users and 1,000 groups-definitely a rough burden to place on a production domain that isn't expecting it!
Preload Domain II-Explained
This script starts by connecting to the root domain.
'create 10,000 user accounts
'seriously - don't run this in a
'production domain!
'connect to the root
Dim oRoot
Set oRoot = GetObject("LDAP://rootDSE")
Next, it gets a reference to the Users container, which is where the new users and groups will be placed.
'connect to the Users container
Dim oContainer
Set oContainer = GetObject("LDAP://cn=Users," & _
oRoot.Get("defaultNamingContext")
Now the script creates 10,000 users, named DummyUser1, DummyUser2, and so forth. Note that they'll all have empty passwords, meaning your domain policies will need to be set to allow a minimum password length of zero. That's not the default in Windows Server 2003 domains.
'create 10,000 users
Dim iUser, oUser
For iUser = 1 To 10000
Set oUser = oContainer.Create("user", _
"DummyUser" & CStr(iUser)
oUser.SetInfo
Next
Next, the script creates 1,000 user groups, named DummyGroup1, DummyGroup2, and so forth.
'create 1,000 groups
Dim iGroup, oGroup
For iGroup = 1 To 1000
Set oGroup = oContainer.Create("group", _
"DummyGroup" & CStr(iGroup)
oGroup.SetInfo
Next
The script next runs through each of the 1,000 groups. I'm using a variable named iLastUser to keep track of the last user I worked with.
'go through the users and place
'1,000 of them in each group
Dim iLastUser
iLastUser = 1
For iGroup = 1 To 1000
For each group, I get an LDAP reference to the group itself.
'get the group
Set oGroup = GetObject("LDAP://cn=DummyGroup" & _
CStr(iGroup) & ",dc=domain,dc=com")
Then, I go through 1,000 users. I preloaded iLastUser with 1, so the first pass will be 1 to 999. After the last Next, iUser will equal 1,000, so the second loop will be 1,000 to 1,999. I add each user's FQDN to the member property of the group.
'go through users
For iUser = iLastUser To iLastUser + 999
oGroup.PutEx 3, "member", _
"cn=DummyUser" & CStr(iUser) & _
",dc=domain,dc=com"
Next
iLastUser = iUser
Next
That's a neat way to quickly load a bunch of data into a domain, so that you can do load testing, application testing, backup and restore testing, or whatever else you need to do. You've seen examples of how to use both the LDAP and WinNT providers to work with users and groups, and you'll continue to see more examples throughout this book. In fact, Chapter 30 contains additional ready-to-run example scripts that focus entirely on Windows and domain administration, and Chapter 20 allows you to combine your knowledge of ADSI and WMI-which is coming up next-to design, write, test, and debug a complete Windows and domain management script.
|