Using Subroutines and Functions in Microsoft VBScript

  • 12/29/2006

One Step Further: Adding a Logging Subroutine

In this section, you add logging capability to the script you finished in the Step-by-Step exercise.

  1. Open Notepad or some other script editor.

  2. Open \My Documents\Microsoft Press\VBScriptSBS\ch15\OneStepFurther\Create MultipleUsersStarter.vbs and save the file as YourNameCreateMultipleUsers Logged.vbs.

  3. After the objGroup.add command statement but before the Loop command, add a call to the subroutine called LogAction. The modification to the script will look like the following:

      Set objGroup = GetObject _
        ("LDAP://CN="& TxtIn(5) & ",cn=users,dc=nwtraders,dc=msft")
      objGroup.Add _
        "LDAP://cn="& TxtIn(0) & ",ou=Mred,dc=nwtraders,dc=msft"
      LogAction
    Loop
  4. Under the ReadUsers subroutine, add a subroutine called LogAction. This will consist of the Sub command and the End Sub command. Leave two blank lines between the two commands. The code will look like the following:

    Sub LogAction
    
    End Sub
  5. Save your work.

  6. Open the \My Documents\Microsoft Press\VBScriptSBS\ch15\OneStepFurther\CreateLogFile.vbs file and copy all the variable declarations. Paste them under the variables in your script.

  7. Delete the extra objFSO variable.

  8. Copy the three reference lines from the CreateLogFile.vbs script and paste them under the variable declarations. This section of the script now looks like the following:

    Dim objOU
    Dim objUser
    Dim objGroup
    Dim objFSO
    Dim objTextFile
    Dim TxtIn
    Dim strNextLine
    Dim i
    Dim TxtFile
    Dim objFile     'holds hook to the file to be used
    Dim message     'holds message to be written to file
    Dim objData1   'holds data from source used to write to file
    Dim objData2   'holds data from source used to write to file
    Dim LogFolder
    Dim LogFile
    message="Reading computer info " & Now
    objData1 = objRecordSet.Fields("name")
    objData2 = objRecordSet.Fields("distinguishedName")
  9. Modify the message so that it states that the code is creating a user, and use the element TxtIn(0) as the user name that gets created. This modified line will look like the following:

    message="Creating user " & TxtIn(0) & Now
  10. Move the message line to the line after you parse strNextLine. You do this because you are using an element of the array that must be an assigned value before it can be used.

    strNextLine = objTextFile.ReadLine
    TxtIn = Split(strNextLine , ",")
    message="Creating user " & TxtIn(1) & Now
  11. Modify the objData1 and objData2 data assignments. Use TxtIn(0) for the user field and TxtIn(5) for the group. The two lines will look like the following:

    objData1 = TxtIn(0)
    objData2 = TxtIn(5)
  12. Copy the remainder of the script and paste it between the two lines used to create the subroutine. The completed section looks like the following:

    Sub LogAction
      If objFSO.FolderExists(LogFolder) Then
        If objFSO.FileExists(LogFile) Then
          Set objFile = objFSO.OpenTextFile(LogFile, ForAppending)
          objFile.WriteBlankLines(1)
          objFile.Writeline message
          objFile.Writeline objData1
          objFile.Writeline objData2
          objFile.Close
        Else
          Set objFile = objFSO.CreateTextFile(LogFile)
          objFile.Close
          Set objFile = objFSO.OpenTextFile(LogFile, ForWriting)
          objfile.WriteLine message
          objFile.WriteLine objData1
          objFile.WriteLine objData2
          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 message
        objFile.WriteLine objData1
        objFile.WriteLine objData2
        objFile.Close
      End If
    End Sub
  13. Save and run the script.

Chapter 15 Quick Reference

To

Do This

Create a subroutine

Begin a line with the word Sub followed by the name of the subroutine. You end the subroutine by using the command End Sub on a line following your subroutine code.

Call a subroutine

Place the name of the subroutine on a line by itself at the place in your code where you want to use the subroutine.

Make code more portable and easier to read and troubleshoot, and to promote code reuse

Use a subroutine.