Creating Detailed Event Log Entry Descriptions

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

When your script writes an event to the event log, it can tap the capabilities of WMI to create more meaningful event descriptions, descriptions that can include information such as the amount of memory installed on a computer or the amount of available disk space on a computer.

For example, the event log might include an error event indicating that an application could not be installed on a computer. This particular application might require 15 MB of free disk space in order to be installed. If the event description includes the fact that the computer had only 10 MB of free disk space, you know at least one reason why the application could not be installed on that computer.

Scripting Steps

Listing 12.15 contains a script that adds WMI data to an event log entry. To carry out this task, the script must perform the following steps:

  1. Create a constant EVENT_FAILED and set the value to 2.

    This indicates that an error occurred.

  2. Create an instance of the WSH Shell object. This is required to use the LogEvent method.
  3. Create a WSH Network object.

    This object is used to return the name of the computer on which the installation was attempted, as well as the domain and user name of the person who attempted the installation.

  4. Create a variable to specify the computer name.
  5. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."
  6. Use the ExecQuery method to query the Win32_ LogicalDisk class.

    This query returns a collection consisting of all the logical disks installed on the computer.

  7. For each logical disk in the collection, retrieve the free disk space by using the FreeSpace property.
  8. Add the drive name, the amount of free space, and a carriage return (VbCrLf) to the variable strDrive Space.

    After the script loops through the entire collection, this variable will contain a list of drives and the available space on each. For example:

    A: 0 C: 152611 D: 5678322 
  9. Set the value of the variable strEventDescription to a string consisting of:
    • The string "Payroll application could not be installed on ".
    • The user domain, computer name, and user name.
    • The string "Free space on each drive is: " and the variable strDriveSpace.

    The final value for strEventDescription might look similar to this:

    Payroll application could not be installed on FABRIKAM\atl-xp-pro-001 by user kmeyer. Free space on each drive is: A: 0 C: 152611 D: 5678322 
  10. Use the LogEvent method to write the record to the event log, specifying the event type and the event description.

Listing 12.15   Adding WMI Data to an Event Log Entry

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
Const EVENT_FAILED = 2 Set objShell = Wscript.CreateObject("Wscript.Shell") Set objNetwork = Wscript.CreateObject("Wscript.Network") strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDiskDrives = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Logicaldisk") For Each objDisk in colDiskDrives    strDriveSpace = strDriveSpace & objDisk.Name & " " & _         objDisk.FreeSpace & VbCrLf Next strEventDescription = "Payroll application could not be installed on " _     & objNetwork.UserDomain & "\" & objNetwork.ComputerName _         & " by user " & objNetwork.UserName & _             ". Free space on each drive is: " & VbCrLf & strDriveSpace objShell.LogEvent EVENT_FAILED, strEventDescription

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net