As networks become more complex, network administrators
can't afford to run from pillar to post doing routine management tasks. The
best approach is to automate as many mundane jobs as possible, so that energy
can be spent on the more important ones. That's where scripts come in. Most
network OS support some scripting language or the other that can be used to
perform routine tasks. Here, we'll take you through one such script for adding
multiple user accounts in the Active Directory of a Windows 2003 Server. We've
given this script on this month's PCQ Live! CD. However, you can find the
entire collection of sample scripts from Microsoft's script repository. The
link for the same is given in the Direct Hit box.
|
Creating users is not a very time consuming task, nor is it
difficult to do. However, if you consider a large enterprise with hundreds of
thousands of users, even this can become a challenge. For instance, somebody new
has joined in the marketing division, another two have joined in accounting, and
yet another three have joined the support team. Instead of adding all six users
one by one, you could simply put them in an Excel file, run a script and they
get added automatically. Better still, if you could link this with your HR
management system, then you'll get details of all the new users in a
predefined format. You'll then simply need to run the script. In this article,
we'll walk you through the sample script's code and tell you how to use it.
The first step of course is to create a common file in
which you'll be storing details of all the users whose accounts need to be
created. Like we said, this could either come from your HR department, or you
could keep a standard template ready, as shown in the screenshot. This file
should have the same format as accepted in the create user form in the Active
Directory (First Name, Initials, Last Name, etc). A point to be noted here is
that the password would remain the same for all users. Thus, you need to set a
warning that the user should change the password immediately after logging in.
Editing the script
Open the sample script given on this month's CD using any text
editor. Go to the line that says 'strExcelPath =' and enter the path of the
Excel file you just created. Next, go to the following line
strContainerDN = "ou=ouname,dc=dcname,dc=com"
Here, you need to change your name with the name of the
Organizational Unit where you want to create users, and in the dc field, you
need to specify the name of the domain controller. That's all you need to do,
after which your script is ready to be run.
![]() |
![]() |
|
Administrators can enter usernames, passwords and other such information in an Excel worksheet for our script's input | This console is the place to come to manage your users. But thanks to our script, this is not necessary. The script adds users under the 'DOMAIN\Builtin' container |
Understanding the code
The script has been written in VB, which is a fairly common scripting
language most system administrators are familiar with. It shouldn't therefore
take you too long to figure out the script. Let's walk through the script to
give you a idea of what's going on in it. The following script opens up the
Excel file. If Excel file's path is wrong or the file can't be found, it
will send an error message- 'unable to open spreadsheet'.
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to open spreadsheet " & strExcelPath
Wscript.Quit
End If
The following part of the script extracts data from the
Excel file one row at a time, and starts putting them in their respective
fields.
intRow = 2
Do While objSheet.Cells(intRow, 5).Value <> ""
strFirstname = Trim(objSheet.Cells(intRow, 1).Value)
strMiddle = Trim(objSheet.Cells(intRow, 2).Value)
strLastname = Trim(objSheet.Cells(intRow, 3).Value)
strPW = Trim(objSheet.Cells(intRow, 4).Value)
strCN = Trim(objSheet.Cells(intRow, 5).Value)
strulnprew2k = Trim(objSheet.Cells(intRow, 6).Value)
strUPN = Trim(objSheet.Cells(intRow, 7).Value)
strHomeFolder = Trim(objSheet.Cells(intRow, 8).Value)
strDepartment = Trim(objSheet.Cells(intRow, 9).value)
strCompany = Trim(objSheet.Cells(intRow , 10).value)
On Error Resume Next
Set objUser = objContainer.Create("user", "cn=" &
strCN) If Err.Number <> 0 Then
On Error GoTo 0
The following part in the script assigns a password to the
newly created user and enables the account by unchecking the option account
disabled.
objUser.SetPassword strPW
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to set
password for user " & strULNprew2k
End If
On Error GoTo 0
objUser.AccountDisabled =
False
If strFirstname <> "" Then
objUser.givenName = strFirstname
End If
Lastly, after the script has finished making the user
accounts, it shows the 'Done' dialogue box and quits Excel.
Wscript.Echo "Done"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
That's all there is to using scripts. Next month, we'll
show you how to use more complex scripts.
Swapnil Arora