NiCE Library write event WA

NiCE.Library.X.Write.Event2.WA (WriteActionModuleType)

WriteAction to write an event in the Operations Manager Event Log.

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
WriteEvent WriteAction Microsoft.Windows.ScriptWriteAction Default

Source Code:

<WriteActionModuleType ID="NiCE.Library.X.Write.Event2.WA" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="EventInformations" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="EventError" type="xsd:string"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="WriteEvent" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>NiCELibraryWriteEvent.vbs</ScriptName>
<Arguments>"$Config/EventInformations$" "$Config/EventError$"</Arguments>
<ScriptBody><Script>'-------------------------------------------------------------------------------
'* VBScript Name: NiCELibraryWriteEvent.vbs
'* Description : Write an Event to the Event Log
'-------------------------------------------------------------------------------
'Declare Global Constants

'Constants used for event logging
Option explicit
SetLocale("en-us")

' Declare Global Variables
Dim oAPI, aEventData, szEventNo, i,szEventInformation,szEventError
Dim Logger

szEventNo = 1

'Start by setting up API object and creating a discovery data object.
Set oAPI = CreateObject("MOM.ScriptAPI")
Set Logger = new NiCELogger.Init("","","false")

'******************************************************************
' MAIN
'******************************************************************
Dim FUNCTIONNAME : FUNCTIONNAME = "MAIN"


If WScript.Arguments.Count &gt; 1 then
szEventInformation = CStr(WScript.Arguments.Item(0))
szEventError = CStr(WScript.Arguments.Item(1))

If szEventInformation = "" and szEventError = "" Then
Call Logger.LogInfo(FUNCTIONNAME,"The Argument is empty. Failed for $MPElement$")
WScript.Echo "The Argument is empty. Failed for $MPElement$"
End If

If szEventInformation &lt;&gt; "" Then
'Expected Format: EventID;EventLevel;EventDescription.
aEventData = Split(szEventInformation,";")
If UBound(aEventData) &gt; 1 Then
Select Case aEventData(1)
Case "Success"
szEventNo = 0
Case "Critical"
szEventNo = 1
Case "Warning"
szEventNo = 2
Case "Information"
szEventNo = 4
End Select

If UBound(aEventData) &gt; 2 Then
For i = 3 to UBound(aEventData)
aEventData(2) = aEventData(2) &amp; ";" &amp; aEventData(i)
Next
End If

aEventData(2) = Replace(aEventData(2),"\n",VbCrLf)


Call oAPI.LogScriptEvent(WScript.ScriptName ,aEventData(0), szEventNo, aEventData(2))
WScript.Echo aEventData(2)
Else
Call Logger.LogInfo(FUNCTIONNAME,"The Argument is invalid. Failed for $MPElement$. Argument: " &amp; szEventInformation)
WScript.Echo "The Argument is invalid. Failed for $MPElement$. Argument: " &amp; szEventInformation
End If
End If

If szEventError &lt;&gt; "" then
Call Logger.LogError(FUNCTIONNAME,szEventError)
WScript.Echo szEventError
End If
Else
Call Logger.LogError(FUNCTIONNAME,"Arguments are missing. Arguments count: " &amp; WScript.Arguments.Count &amp; ". Failed for $MPElement$")
WScript.Echo "Arguments are missing. Arguments count: " &amp; WScript.Arguments.Count &amp; ". Failed for $MPElement$"
End If

'******************************************************************
' Classes
'******************************************************************

'==================================================================================
' Class: NiCELogger
' Usage: Set Logger = new NiCELogger.Init("NiCEDataDirectory","ProductName","Tracing")
' Set Logger = new NiCELogger.Init("","","Tracing")
' Call Logger.LogDebug("funcname","message")
' Call Logger.LogError("funcname","message")
' Call Logger.LogWarning("funcname","message")
'==================================================================================
Class NiCELogger
Private m_TracingEnabled
Private m_ProductName
Private m_NiCEDataDirectory
Private m_LogDirectory
Private m_ConfDirectory
Private m_LogFileName
Private m_objFSO
Private m_WshShell
Private m_oAPI

'==================================================================================
' Property: TracingEnabled
' Returns: true or false
'==================================================================================
Public Property Get TracingEnabled
TracingEnabled = m_TracingEnabled
End Property

Public Default Function Init(NiCEDataDirectory, ProductName, Tracing)
Set m_objFSO = CreateObject("Scripting.FileSystemObject")
Set m_oAPI = CreateObject("MOM.ScriptAPI")
Set m_WshShell = WScript.CreateObject("WScript.Shell")
m_TracingEnabled = false

If InStr(1,Tracing,"True", vbTextCompare) Then
m_TracingEnabled = true
End If

m_LogFileName = Replace(WScript.ScriptName,".vbs","_Trace.log")
m_NiCEDataDirectory = RemoveSlash(CStr(NiCEDataDirectory))
m_ProductName = CStr(ProductName)
if m_ProductName &lt;&gt; "" And m_NiCEDataDirectory &lt;&gt; "" Then
m_LogDirectory = m_NiCEDataDirectory &amp; "\log\" &amp; m_ProductName &amp; "\"
m_ConfDirectory = m_NiCEDataDirectory &amp; "\conf\" &amp; m_ProductName &amp; "\"
Else
m_LogDirectory = RemoveSlash(m_WshShell.ExpandEnvironmentStrings("%temp%")) &amp; "\"
m_ConfDirectory = RemoveSlash(m_WshShell.ExpandEnvironmentStrings("%temp%")) &amp; "\"
End If

Call InitSpiCfg()

Set Init = Me
End Function

Private Sub InitSpiCfg()
Dim FUNCTIONNAME : FUNCTIONNAME = "NiCELogger::InitSpiCfg"
If m_TracingEnabled = false Then
Dim SpiCfgobjFile, SpiCfgFile, szSpiCfgLine
Dim TraceEnabled : TraceEnabled = false
Dim TraceProcess : TraceProcess = false
Dim SpiCfgPath : SpiCfgPath = m_ConfDirectory &amp; m_ProductName &amp; "_spi.cfg"

If not m_objFSO.FileExists(SpiCfgPath) Then
Dim temppath : temppath = RemoveSlash(m_WshShell.Environment("System")("TEMP"))
SpiCfgPath = temppath &amp; "\" &amp; m_ProductName &amp; "_spi.cfg"
m_LogDirectory = temppath &amp; "\"
m_ConfDirectory = temppath &amp; "\"
End If

If m_objFSO.FileExists(SpiCfgPath) Then
Set SpiCfgobjFile = m_objFSO.GetFile(SpiCfgPath)
If SpiCfgobjFile.Size &gt; 0 Then
Set SpiCfgFile = m_objFSO.OpenTextFile(SpiCfgPath,1)
Do While SpiCfgFile.AtEndOfStream &lt;&gt; True
szSpiCfgLine = SpiCfgFile.ReadLine
If InStr(1,szSpiCfgLine,"SPI_TRACE_STATUS", vbTextCompare) and InStr(1,szSpiCfgLine,"ON", vbTextCompare) Then
TraceEnabled = true
End If

If InStr(1,szSpiCfgLine,"SPI_TRACE_PROCESS", vbTextCompare) and (InStr(1,szSpiCfgLine,"ALL", vbTextCompare) or InStr(1,szSpiCfgLine,WScript.ScriptName, vbTextCompare)) Then
TraceProcess = true
End If
Loop
End If
End If

If TraceEnabled = true and TraceProcess = true Then
m_TracingEnabled = true
End If
End If
End Sub

'==================================================================================
' Sub: LogError
' Purpose: Logs an error event to the operations manager event log
'==================================================================================
Public Sub LogError(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6121,1,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "ERROR", szMessage)
End Sub

'==================================================================================
' Sub: LogWarning
' Purpose: Logs a warning event to the operations manager event log
'==================================================================================
Public Sub LogWarning(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6122,2,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "WARNING", szMessage)
End Sub

'==================================================================================
' Sub: LogInfo
' Purpose: Logs an info event to the operations manager event log
'==================================================================================
Public Sub LogInfo(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6123,4,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "INFO", szMessage)
End Sub

'==================================================================================
' Sub: LogDebug
' Purpose: Logs a debug message in the log file
'==================================================================================
Public Sub LogDebug(FUNCTIONNAME, szMessage)
Call LogDebugInternal(FUNCTIONNAME, "DEBUG", szMessage)
End Sub

Private Sub LogDebugInternal(FUNCTIONNAME, szServerity, szMessage)
If m_TracingEnabled = True Then
Dim LogFile
szMessage = Now &amp; " - " &amp; szServerity &amp; " - " &amp; FUNCTIONNAME &amp; " - " &amp; szMessage
If not m_objFSO.FolderExists(m_LogDirectory) then
CreateFolders(m_LogDirectory)
End If

If m_objFSO.FileExists(m_LogDirectory &amp; m_LogFileName) Then
Set LogFile = m_objFSO.OpenTextFile(m_LogDirectory &amp; m_LogFileName,8)
Call LogFile.WriteLine(szMessage)
Else
Set LogFile = m_objFSO.CreateTextFile(m_LogDirectory &amp; m_LogFileName,true)
Call LogFile.WriteLine(szMessage)
End If
End If
End Sub

'==================================================================================
' Sub: LogEvent
' Purpose: Logs an event to the Operations Manager event log
'==================================================================================
Private Sub LogEvent(FUNCTIONNAME,EventNumber, EventLevel, szMessage)
szMessage = FUNCTIONNAME &amp; VbCrLf &amp; szMessage
Dim nLength, nNum, nStart, szNewMessage, i
nLength = Len(szMessage) / 30000
nNum = CInt(nLength + 0.5)

If nNum &gt; 1 Then ' we have more than 30000 chars, split them into multiple parts'
For i = 0 to nNum-1
szNewMessage = "Part " &amp; i+1 &amp; "/" &amp; nNum &amp; VbCrLf
nStart = 1+(i*30000)
szNewMessage = szNewMessage &amp; Mid(szMessage, nStart, 30000)
Call m_oAPI.LogScriptEvent(WScript.ScriptName, EventNumber, EventLevel, szNewMessage)
Next
Else
Call m_oAPI.LogScriptEvent(WScript.ScriptName, EventNumber, EventLevel, szMessage)
End If
End Sub

'==================================================================================
' Function: RemoveSlash
' Purpose: removes the last char of a string if it a forward or backward slash
'==================================================================================
Public Function RemoveSlash(Path)
Dim szLastChar
szLastChar = Right(Path,1)
If szLastChar = "\" OR szLastChar = "/" Then
removeSlash = Left(Path,Len(Path)-1)
Else
removeSlash = Path
End If
End Function

Public Sub CreateFolders(Path)
Dim FolderArray : FolderArray = Split(Path, "\")
Dim Folder,i

For i=0 to UBound(FolderArray)
If i &lt;&gt; 0 Then
Folder = FolderArray(i-1)
End If

Folder = Folder &amp; FolderArray(i) &amp; "\"

If not m_objFSO.FolderExists(Folder) Then
m_objFSO.CreateFolder(Folder)
End If

FolderArray(i) = Folder
Next
End Sub
End Class


'******************************************************************
' Subs and Functions
'******************************************************************
</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WriteEvent"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>