Script that Returns Multiple Lines

I saw a couple of questions about a script searching for a word in a logfile and then create a event with the line including the “word” but also the two lines before that. This script search for the word “Warning” in the file logfile.txt. If found it will generate a local event with the line includning Warning but also the two lines before that.

Const ForReading = 1
Const EVENT_WARNING = 2Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Warning"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("logfile.txt", ForReading)

strLineNo = 1

Do Until objFile.AtEndOfStream

     strSearchString = objFile.ReadLine
     Set colMatches = objRegEx.Execute(strSearchString)

    strCacheC = strCacheB
  strCacheB = strCacheA
  strCacheA = strSearchString

   
 If colMatches.Count > 0 Then
        For Each strMatch in colMatches  
          
 Set objShell = CreateObject("Wscript.Shell")
 objShell.LogEvent EVENT_WARNING, _
 "Warning from logfile: " & vbCr &_
 "BEGINNING OF LINES" & vbCr &_
 "" & vbCr &_
  strCacheC & vbCr &_
         strCacheB & vbCr &_
         strCacheA & vbCr &_
 "" & vbCr &_
         "END OF LINES."
        Next

 End If   

strLineNo = strLineNo + 1

Loop
objFile.Close

About

Microsoft

1 comment Categories: Scripts

One thought on “Script that Returns Multiple Lines

Comments are closed.