Home > Sample chapters

Using Subroutines and Functions in Microsoft VBScript

Creating Users and Logging Results

As your scripts become more powerful, they have a tendency to become longer and longer. The next script, CreateUsersLogAction.vbs, is nearly 80 lines long. The reason for this length is that you perform three distinct actions. First, you read a text file and parse the data into an array. Then you use this array to create new users and add the users into an existing group in Active Directory. As you create users and add them to groups, you want to create a log file and write the names of the created users. All the code to perform these actions begins to add up and can make a long script hard to read and understand. The subroutine becomes rather useful in such a situation. In fact, the subroutine used to create the log file is nearly 30 lines long itself because you need to check whether the folder exists or the log file exists. If the folder or file does not exist, you need to create it. If each is present, you need to open the file and append data to it. By placing this code into a subroutine, you are able to access it each time you loop through the input data you’re using to create the users in the first place. After the user is created, you go to the subroutine, open the file, write to it, close the file, and then go back into Do Until...Loop to create the next user.

If you kept the file open and wrote to the log file in an asynchronous manner, your log writer could get behind, and in the event of an anomaly, your log might not be an accurate reflection of the actual accounts created on the server. Here is the CreateUsersLogAction.vbs script.

CreateUsersLogAction.vbs

Option Explicit
On Error Resume Next
Dim objOU
Dim objUser
Dim objGroup
Dim objFSO
Dim objFile
Dim objFolder
Dim objTextFile
Dim TxtIn
Dim strNextLine
Dim i
Dim TxtFile
Dim LogFolder
Dim LogFile

TxtFile = "C:\UsersAndGroups.txt"
LogFolder = "C:\FSO"
LogFile = "C:\FSO\fso.txt"
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
  (TxtFile, ForReading)
Do Until objTextFile.AtEndOfStream
  strNextLine = objTextFile.ReadLine
  TxtIn = Split(strNextLine , ",")
  Set objOU = GetObject("LDAP://OU=mred," _
    & "dc=nwtraders,dc=msft")
  Set objUser = objOU.Create("User", "cn="& TxtIn(0))
  objUser.Put "sAMAccountName", TxtIn(0)
  objUser.SetInfo

  Set objGroup = GetObject _
    ("LDAP://CN="& TxtIn(1) & ",cn=users," _
    & "dc=nwtraders,dc=msft")
  objGroup.add _
    "LDAP://cn="& TxtIn(0) & ",ou=Mred," _
    & "dc=nwtraders,dc=msft"
  Logging
Loop

Sub Logging
  If objFSO.FolderExists(LogFolder) Then
    If objFSO.FileExists(LogFile) Then
      Set objFile = objFSO.OpenTextFile _
         (LogFile, ForAppending)
      objFile.WriteBlankLines(1)
      objFile.Writeline "Creating User " & Now
      objFile.Writeline TxtIn(0)
      objFile.Close
    Else
      Set objFile = objFSO.CreateTextFile(LogFile)
      objFile.Close
      Set objFile = objFSO.OpenTextFile _
         (LogFile, ForWriting)
      objFile.WriteLine "Creating User " & Now
      objFile.WriteLine TxtIn(0)

      objFile.Close
    End If
  Else
    Set objFolder = objFSO.CreateFolder(LogFolder)
    Set objFile = objFSO.CreateTextFile(LogFile)
    objFile.Close
    Set objFile = objFSO.OpenTextFile _
       (LogFile, ForWriting)
    objfile.WriteLine "Creating User " & Now
    objFile.WriteLine TxtIn(0)
    objFile.Close
  End If
End Sub

WScript.Echo("all done")