Previous Page
Next Page

One Step Further: Adding Logging to a Logon Script

In this section, you add logging to the AddGroupToLogonScript.

1.
Open up the \My Documents\Microsoft Press\VBScriptSBS\ch16\OneStepFurther\LoggedLogonScriptStarter.vbs script in Notepad or your favorite script editor and save the file as YourNameLoggedLogonScript.vbs.

2.
Copy the declared variables from the CreateLogFile.vbs file in the One Step Further folder and paste them into the Header information section of your script. The new Header information section of the script looks like the following:

Option Explicit
Dim fServer
Dim home
Dim wshNet
Dim ADSysInfo
Dim CurrentUser
Dim strGroups
Dim GroupMember
Dim objFSO      'holds connection to file system object
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

3.
Copy the entire Reference information section of the CreateLogFile.vbs file, including all the constants and variable assignments. Paste this under the constants in your script. The completed section looks like this:

Const HR = "cn=hrgroup"
Const MARKETING = "cn=marketinggroup"
Const SALES = "cn=salesgroup"
Const PRODUCTION = "cn=productiongroup"
Const ForWriting = 2
Const ForAppending = 8
LogFolder = "C:\fso"
LogFile = "C:\fso\logFile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
message="Reading computer info " & Now
objData1 = objRecordSet.Fields("name")
objData2 = objRecordSet.Fields("distinguishedName")

4.
Change the message text so that it reads "Processing Logon Script".

5.
Cut the objData1 and objData2 variables and paste them under the strGroups = LCase line. This section of the script now looks like the following:

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))
objData1 = objRecordSet.Fields("name")
objData2 = objRecordSet.Fields("distinguishedName")
wshNet.MapNetworkDrive "h:", fServer & "\Users\" & wshNet.UserName
WScript.Echo(wshNet.Username & " " & strgroups)

6.
Assign values to objData1 and objData2. Make objData1 equal to ADSysInfo.UserName and objData2 equal to strGroups. The two modified objData lines now look like the following:

objData1 = ADSysInfo.UserName
objData2 = strGroups

7.
At the bottom of the subroutines in your script, create a new empty subroutine called LoggingSub.

8.
Inside the empty LoggingSub subroutine, paste the entire If...Then...End If section from the CreateLogFile.vbs file. The completed LoggingSub subroutine now looks like the following:

Sub LoggingSub
  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

9.
Save your work.

10.
In the HRSub subroutine, add a command to go to the LoggingSub subroutine after the setDefaultPrinter command. The new HRSub subroutine now looks like the following:

Sub HRsub
  WScript.Echo("made it to HR")
  wshNet.MapNetworkDrive "g:","\\london\Hr\"
  wshNet.AddWindowsPrinterConnection "\\london\HrPrinter"
  wshNet.SetDefaultPrinter "\\london\HrPrinter"
  LoggingSub
End Sub

11.
Add the LoggingSub command to the end of the salesSub, marketingSub, and productionSub subroutines as well. The completed subroutines look like the following:

Sub SalesSub
  WScript.Echo("made it to sales")
  wshNet.MapNetworkDrive "s:", "\\london\Sales"
  wshNet.AddWindowsPrinterConnection "\\london\SalesPrinter"
  wshNet.SetDefaultPrinter "\\london\SalesPrinter"
  Loggingsub
End Sub

Sub MarketingSub
WScript.Echo("made it to marketing")
  wshNet.MapNetworkDrive "m:","\\london\Marketing\"
  wshNet.AddWindowsPrinterConnection "\\london\MarketingPrinter"
  wshNet.SetDefaultPrinter "\\london\MarketingPrinter"
  Loggingsub
End Sub

Sub ProductionSub
  WScript.Echo("made it to production")
  wshNet.MapNetworkDrive "p:","\\london\Production\"
  wshNet.AddWindowsPrinterConnection "\\london\ProductionPrinter"
  wshNet.SetDefaultPrinter "\\london\ProductionPrinter"
  Loggingsub
End Sub

12.
Save your work and test your script by logging on to the test domain from a remote machine. If you have problems with your script, compare it to \My Documents \Microsoft Press\VBScriptSBS\ch16\OneStepFurther\LoggedLogonScript Solution.vbs.

Chapter 16 Quick Reference

To

Do This

Obtain user and computer information that returns the distinguished user name, which can then bind to Active Directory and perform queries

Use ADSysInfo as opposed to WshNetwork

Put elements of an array together into a single string

Use the Join function

Map to network shares and network printers and remove network shares and network printers

Use WshNetwork

Refresh the local copy of the Active Directory schema on a computer

Use RefreshSchemaCache



Previous Page
Next Page