Using Subroutines and Functions in Microsoft VBScript

  • 12/29/2006

Using ADSI and Subs, and Creating Users Step-by-Step Exercises

In this section, you will expand the script used in this chapter. Instead of creating only a user, you will add information to the user. You will use a subroutine to perform logging.

  1. Open Notepad or your favorite script editor.

  2. Open the \My Documents\Microsoft Press\VBScriptSBS\ch15\StepByStep\CreateUsers.vbs file and save it as YourNameCreateMultipleUsersSolution.vbs.

  3. Make sure you have a file called C:\fso\UsersAndGroups.txt, and run the CreateUsers.vbs file. Go into Active Directory Users And Computers (ADUC) and delete the users that were created.

  4. Cut the code used to open the text file that holds the names of users to add to Active Directory. It is under the variable declarations, in the Reference information section of the script. It is five lines long. This code looks like the following:

    TxtFile = "C:\fso\UsersAndGroups.txt"
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
      (TxtFile, ForReading)
  5. Paste the code after the WScript.Echo command at the end of the script.

  6. Under the declarations, where the txtFile code used to be, type ReadUsers. This is the name of the new subroutine you will create. It will look like the following:

    Dim objOU
    Dim objUser
    Dim objGroup
    Dim objFSO
    Dim objTextFile
    Dim TxtIn
    Dim strNextLine
    Dim i
    Dim TxtFile
    dim boundary
    
    ReadUsers
  7. On the line before the code that reads TxtFile, which you copied to the end of your script, use the Sub command to create a subroutine called ReadUsers.

  8. At the end of the subroutine, add the End Sub command. The completed subroutine looks like the following:

    Sub ReadUsers
      TxtFile = "c:\fso\UsersAndGroups.txt"
      Const ForReading = 1
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objTextFile = objFSO.OpenTextFile _
        (TxtFile, ForReading)
    End Sub
  9. Save your work. Run the script to make sure it still works. Open Active Directory Users And Computers and delete the users that were created by running the script.

  10. Modify the subroutine so that it is reading a text file called MoreUsersAndGroups.txt. This file is located in the \My Documents\Microsoft Press\VBScriptSBS\ch15\Step ByStep folder.

  11. In the Worker section of the script that creates the user, use the Put method to add the user’s first name, last name, building, and phone number. The Active Directory attributes are called givenName, sn, physicalDeliveryOfficeName, and telephoneNumber. Each of these fields is in the array that gets created, so you need to increment the array field. The completed code will look like the following:

    Set objUser = objOU.Create("User", "cn="& TxtIn(0))
    objUser.Put "sAMAccountName", TxtIn(0)
    objUser.Put "givenName", TxtIn(1)
    objUser.Put "sn", TxtIn(2)
    objUser.Put "physicalDeliveryOfficeName", TxtIn(3)
    objUser.Put "telephoneNumber", TxtIn(4)
  12. Because the group membership field is the last field and you added fields to the text file, you need to increment the array index that is used to point to the group field. The new index number is 5, and the code will look like the following:

    Set objGroup = GetObject _
      ("LDAP://CN="& TxtIn(5) & ",cn=users,dc=nwtraders,dc=msft")
  13. Save the script and run it. After you successfully run the script, delete the users created in Active Directory.