'##########################################################################
' Class: Util
' Description: Contains methods for tracing, generating events,
' creating objects
'##########################################################################
Class Util
' Used to say to LogMessage when/how to print the message.
Public DBG_NONE
Public DBG_ERROR
Public DBG_WARNING
Public DBG_TRACE
Public HKEY_LOCAL_MACHINE
Public MOMApiObject
'Internal Debug Level
Private m_nDebugLevel
'Name of the logfile for tracing
Private m_logFileName
'---------------
' Properties
'---------------
Public Property Get LogFileName
LogFileName = m_logFileName
End Property
Public Property Let LogFileName(ByVal fileName)
If Not (IsEmpty(filename) OR IsNull(fileName)) Then
m_logFileName = fileName
Else
sErrObj.GenerateMOMErrorEvent("Log file name cannot be empty or null")
End If
End Property
'---------------
' Methods
'---------------
'=============
' Method: Class_Initialize
' Description: This is the constructor
'=============
Private Sub Class_Initialize()
' Initialize Debug level constants
DBG_TRACE = 1
DBG_WARNING = 2
DBG_ERROR = 3
DBG_NONE = 4
'by default only errors are logged
m_nDebugLevel = DBG_ERROR
'Create a MOM Script API object
Set MOMApiObject = MomCreateObject("MOM.ScriptAPI")
End Sub
'=============
' Method: Class_Terminate
' Description: This is the destructor
'=============
Private Sub Class_Terminate()
Set MOMApiObject = Nothing
End Sub
'=============
' Method: SetDebugLevel
' Description: To change the debugging output level of information
' generated by this utility.
' Parameters: nLevel - Level, either DBG_NONE, DBG_TRACE,
' DBG_WARNING or DBG_ERROR
'=============
Public Sub SetDebugLevel(ByVal nLevel)
m_nDebugLevel = nLevel
End Sub
'=============
' Method: WriteToTrace
' Description: Writes text to a trace file. For every run of the script,
' the trace file is recreated.
' Parameters: msg - Any text that needs to be written to the trace file.
'=============
Public Sub WriteToTrace (ByVal msg)
Dim fso, traceFileObj, objErr
Set objErr = new Error
If Not m_nDebugLevel = DBG_TRACE Then
Exit Sub
End If
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.number <> 0 Then objErr.PrintError
On Error Goto 0
'Checking whether the trace file is recreated in this run
If Not sBlnTraceRefreshed Then
On Error Resume Next
Set traceFileObj = fso.OpenTextFile(sUtilObj.LogFileName, conForWriting, True)
If Err.number <> 0 Then objErr.PrintError
On Error Goto 0
sBlnTraceRefreshed = True
Else
On Error Resume Next
Set traceFileObj = fso.OpenTextFile(sUtilObj.LogFileName, conForAppending, True)
If Err.number <> 0 Then objErr.PrintError
On Error Goto 0
End If
msg = FormatDateTime(Date(), vbShortDate) & " " & _
FormatDateTime(Time(), vbLongTime) & "[" & _
WScript.ScriptName & "]--> " & msg
On Error Resume Next
traceFileObj.WriteLine(msg)
If Err.number <> 0 Then objErr.PrintError
On Error Goto 0
traceFileObj.Close
Set traceFileObj = Nothing
Set fso = Nothing
End Sub
'=============
' Method: MomCreateObject
' Description: Creates an Object and includes neccessary error handling
' Parameters: sProgramId - The Program ID of the object type to be created.
' Returns: Returns the object that is created.
'=============
Public Function MomCreateObject(ByVal sProgramId)
Dim errMsg
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
If Err.number <> 0 Then
errMsg = "Unable to create automation object '" & sProgramID & "'" & "--" & _
"Error- Number: " & Err.number & " Description:" & Err.Description & _
" Source:" & Err.Source
If lcase(sProgramId) = lcase("MOM.ScriptAPI") Then
CreateOpsMgrEvent 312, WScript.ScriptName, "ERROR", errMsg
ThrowEmptyDiscoveryData()
Quit()
Else
sErrObj.GenerateMOMErrorEvent(errMsg)
End If
End If
End Function
'=============
' Method: CreateOpsMgrEvent
' Description: Create an event in the Operations Manager log
' Parameters: eventId - ID of the event to be created
' source - Source of this event
' eventType - String specifying the type of event (ERROR, INFORMATION, SUCCESS, WARNING)
' desc - Description of event.
' Returns: Returns the object that is created.
'=============
Public Sub CreateOpsMgrEvent(ByVal eventId, ByVal source, ByVal eventType, ByVal desc)
Dim strCommand, WshShell
On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "cmd /c eventcreate /ID " & eventID & _
" /SO " & """" & source & """" & _
" /T " & """" & eventType & """" & _
" /D " & """" & desc & """" & _
" /L " & """Operations Manager"""
WshShell.Run strCommand
If Err.number <> 0 Then WScript.Echo "Error in 'Util.CreateOpsMgrEvent()'"
End Sub
End Class
'##########################################################################
' Class: Error
' Description: Contains methods to Save Error details, Raise Error,
' Clear Error
' Assumptions: Util Object is created and available to use. It should be named
' as "sUtilObj"
'##########################################################################
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Private ERROR_FILE_NOT_FOUND
Public Sub Class_Initiaze()
ERROR_FILE_NOT_FOUND = 2
End Sub
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
Public Sub PrintError
WScript.Echo "Error writing to trace." & _
"Error: Number-" & Err.number & _
"; Description-" & Err.Description & _
"; Source-" & Err.Source
End Sub
'=============
' Method: GenerateMOMErrorEvent
' Description: Uses the "MOM Script API" object to log a script event. Appends the Error
' details to the message sent as parameter
' Parameters: strMessage - contains the custom text to write to the event
'=============
Public Function GenerateMOMErrorEvent(ByVal strMessage)
strMessage = strMessage & " Error - Number:" & m_lNumber & _
" Source:" & m_sSource & _
" Description:" & m_sDescription & _
" HelpContext:" & m_sHelpContext & _
" HelpFile:" & m_sHelpFile
On Error Resume Next
CreateScriptErrorEvent(strMessage)
Quit()
End Function
'=============
' Method: CreateScriptErrorEvent
' Description: Generate a MOM event with script error message in the
' Operations Manager Log.
' Parameters: strMessage - Message to write to the event
'=============
Public Function CreateScriptErrorEvent(ByVal strMessage)
On Error Resume Next
sUtilObj.MOMApiObject.LogScriptEvent WScript.ScriptName, 4001, 1, strMessage
End Function
'=============
' Method: ErrorCheck
' Description: Checks if an error occurred and writes the error to Operations Manager
' event log.
'=============
Public Sub ErrorCheck()
If Err.number <> 0 Then
Save
GenerateMOMErrorEvent("")
End If
End Sub
Public Sub ErrorCheckMsg(ByVal msg)
If Err.number <> 0 Then
Save
GenerateMOMErrorEvent(msg)
End If
End Sub
Dim objFPCRoot, objFPCServer, arrArgs, startOrStop
Set sUtilObj = new Util
Set sErrObj = new Error
Set arrArgs = Wscript.Arguments
If arrArgs.Count >= 1 AND arrArgs.Count <= 2 Then
If arrArgs.Count = 2 Then
'Check if Trace is ON
If (lcase(trim(arrArgs(1))) = lcase(conTraceOn)) Then
'Set the debug level to Trace
sUtilObj.SetDebugLevel(sUtilObj.DBG_TRACE)
'Set the trace log file name
sUtilObj.LogFileName = WScript.ScriptName & ".trace"
End If
End If
startOrStop = CInt(arrArgs(0))
Else
sErrObj.GenerateMOMErrorEvent("Invalid number of arguments passed to script.")
End if
sUtilObj.WriteToTrace "Start"
Set objFPCRoot = sUtilObj.MomCreateObject("FPC.Root")
On Error Resume Next
Set objFPCServer = objFPCRoot.GetContainingServer()
sErrObj.ErrorCheckMsg "Error retrieving FPCServer"
On Error Goto 0
Select Case startOrStop
Case conStartISAFirewallService
objFPCServer.StartFirewallService()
sUtilObj.WriteToTrace "Start TMG Firewall Service"
Case conStopISAFirewallSerive
objFPCServer.StopFirewallService()
sUtilObj.WriteToTrace "Stop TMG Firewall Service"
Case Else
sErrObj.GenerateMOMErrorEvent "Invalid value of argument 'TaskToPerform':" & startOrStop & " The value should be 1(Start) or 2(Stop)."
End Select
sUtilObj.WriteToTrace "End"
Set objFPCServer = Nothing
Set objFPCRoot = Nothing
Set sUtilObj = Nothing
Set sErrObj = Nothing
End Sub