Hi.

I'm not a programmer. I do script just to ease my work, and it funs too!!!

This is my project. I made a script to scan a list of server from a file,
retrieve all Critical event message from EV and write it to a file for
analysys with Excel. I should help us configuring our Monitoring application.
The problem that I have is with the "objEvent.Message" field. I want to
revome all formating, so I can export it in Excel and have a nice looking
(Message = 1 cell). Because with all formating character (carriage return,
line feed, etc..) it looks weird when I export it.

Can you help me with my patern maching.
Here is the Scirpt.


'***********************************************************************
'*
'* Names : List All System Event Type Error.vbs
'*
'* Retrieve all Critical error from a list (input file) and write it
'* in an output file for analyses. Input file is in the current
'* directory.
'*
'* By :
'* Date :
'*
'***********************************************************************

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim strComputer
Dim objFSO
Dim strScriptFullName
Dim objFile
Dim objFile2
Dim strFileParentFolder
Dim strInputFileName
Dim strOutputFileName
Dim Message
Dim Message1

strInputFileName = "Input.txt"
strOutputFileName = "Output.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
strScriptFullName = WScript.ScriptFullName
Set objFile = objFSO.GetFile(strScriptFullName)
strFileParentFolder = objFSO.GetParentFolderName(objFile)
Set objFile = objFSO.OpenTextFile(strFileParentFolder & "\" &
strInputFileName, ForReading)
Set objFile2 = objFSO.CreateTextFile(strFileParentFolder & "\" &
strOutputFileName, True)
objFile2.WriteLine "Category,Computer Name,Event Code,Message,Record
Number,Source Name,Time Written,Event Type,User"

Do While objFile.AtEndOfStream = False
strComputer = objFile.ReadLine
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer "\root\cimv2")
Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
& "Type = 'Error'")
For Each objEvent in colLoggedEvents
Message = ReplaceRegX(objEvent.Message, [VbCrLf|vbCr|vbLf|^\.], "")
WScript.Echo Message
' objFile2.WriteLine objEvent.Category & "," & objEvent.ComputerName & ","
& objEvent.EventCode _
' & "," & objEvent.RecordNumber & "," & objEvent.SourceName & "," &
objEvent.TimeWritten _
' & "," & objEvent.Type & "," & objEvent.User & "," & objEvent.Message
Next
Loop

objFile.Close
objFile2.Close

Function ReplaceRegX(str1, patrn, replStr)
Dim regEx ' Create variables.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
ReplaceRegX = regEx.Replace(str1, replStr) ' Make replacement.
End Function


Salutation,
Thank you in advance.

--
jasmin leroux
MCSE Tech. deployement

RE: Clean event viewer log with Regular expression. by jasminleroux

jasminleroux
Thu Nov 09 15:29:02 CST 2006

Just want to add more detail. I did some search. I will redo my question.

RegEx must clean the string, just keep the alpha numeric caracter, so
remove all non alpha-
numeric carater, remove leading blank line and remove ending blank line.

Thanks


--
jasmin leroux
MCSE Tech. deployement


"jasmin leroux" wrote:

> Hi.
>
> I'm not a programmer. I do script just to ease my work, and it funs too!!!
>
> This is my project. I made a script to scan a list of server from a file,
> retrieve all Critical event message from EV and write it to a file for
> analysys with Excel. I should help us configuring our Monitoring application.
> The problem that I have is with the "objEvent.Message" field. I want to
> revome all formating, so I can export it in Excel and have a nice looking
> (Message = 1 cell). Because with all formating character (carriage return,
> line feed, etc..) it looks weird when I export it.
>
> Can you help me with my patern maching.
> Here is the Scirpt.
>
>
> '***********************************************************************
> '*
> '* Names : List All System Event Type Error.vbs
> '*
> '* Retrieve all Critical error from a list (input file) and write it
> '* in an output file for analyses. Input file is in the current
> '* directory.
> '*
> '* By :
> '* Date :
> '*
> '***********************************************************************
>
> Const ForReading = 1
> Const ForWriting = 2
> Const ForAppending = 8
>
> Dim strComputer
> Dim objFSO
> Dim strScriptFullName
> Dim objFile
> Dim objFile2
> Dim strFileParentFolder
> Dim strInputFileName
> Dim strOutputFileName
> Dim Message
> Dim Message1
>
> strInputFileName = "Input.txt"
> strOutputFileName = "Output.txt"
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> strScriptFullName = WScript.ScriptFullName
> Set objFile = objFSO.GetFile(strScriptFullName)
> strFileParentFolder = objFSO.GetParentFolderName(objFile)
> Set objFile = objFSO.OpenTextFile(strFileParentFolder & "\" &
> strInputFileName, ForReading)
> Set objFile2 = objFSO.CreateTextFile(strFileParentFolder & "\" &
> strOutputFileName, True)
> objFile2.WriteLine "Category,Computer Name,Event Code,Message,Record
> Number,Source Name,Time Written,Event Type,User"
>
> Do While objFile.AtEndOfStream = False
> strComputer = objFile.ReadLine
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer "\root\cimv2")
> Set colLoggedEvents = objWMIService.ExecQuery _
> ("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
> & "Type = 'Error'")
> For Each objEvent in colLoggedEvents
> Message = ReplaceRegX(objEvent.Message, [VbCrLf|vbCr|vbLf|^\.], "")
> WScript.Echo Message
> ' objFile2.WriteLine objEvent.Category & "," & objEvent.ComputerName & ","
> & objEvent.EventCode _
> ' & "," & objEvent.RecordNumber & "," & objEvent.SourceName & "," &
> objEvent.TimeWritten _
> ' & "," & objEvent.Type & "," & objEvent.User & "," & objEvent.Message
> Next
> Loop
>
> objFile.Close
> objFile2.Close
>
> Function ReplaceRegX(str1, patrn, replStr)
> Dim regEx ' Create variables.
> Set regEx = New RegExp ' Create regular expression.
> regEx.Pattern = patrn ' Set pattern.
> ReplaceRegX = regEx.Replace(str1, replStr) ' Make replacement.
> End Function
>
>
> Salutation,
> Thank you in advance.
>
> --
> jasmin leroux
> MCSE Tech. deployement