Navigation : DEVELOPPEMENT > VBS, WSH
MATCHER UN LOG ET ENVOYER UN MAIL
Ce script permet de tester une expression regulière sur un fichier text (ici un log), puis d'envoyer un mail à l'administrateur (alerte) si l'expression recherchée est présente dans le fichier.
www.polyteknique.netLE SCRIPT
Dim BinaryStream, RegEx

'Objects
Set BinaryStream = CreateObject("ADODB.Stream")
Set RegExp = New RegExp

'Stream
BinaryStream.Type = 2
BinaryStream.Charset = "utf-8"
BinaryStream.Open
BinaryStream.LoadFromFile "\\path\to\log\TRACE.LOG"
ReadTextFile = BinaryStream.ReadText

'format du log à matcher : 25/05/2006-10:50:24 Derniers fichiers non integres

today = date ' On regarde le log du jour ^^

'RegExp
RegExp.Pattern = "(" & today & "-[0-9:]+ Derniers fichiers non integres)"
RegExp.Multiline = True
RegExp.IgnoreCase = True

retVal = RegExp.Test(ReadTextFile)

If retVal Then
   RegExpTest = 1 'OK
Else
   RegExpTest = 0 'KO
End If

'Mail

If RegExpTest = 1 then

     Set WshShell = WScript.CreateObject("WScript.Shell")

     '<---Envoi_MAIL_CDO.VBS avec Serveur virtuel SMTP installé --->
     ' Nécessite Windows 2000/XP ou une installation Microsoft Office (Outlook).
     ' Dans From Adresse de l'expéditeur
     ' Dans To spécifie l'adresse du destinataire
     ' Dans Subject spécifie le sujet du mail
     ' Dans TextBody ou TMLBody Inscris le corps du mail
     ' Dans AddAttachment Inscris l'adresse de la PJ à envoyer
     '
     '
     With CreateObject("CDO.Message")
          .From="admin@toto.com"
          .To="service-informatique@uneboite.com"
          .Subject="Fichier non intégré"
          .TextBody="Derniers fichiers non integres sur la machine X !"
          On Error Resume Next
          .Send
          If Err Then MsgBox "Le message n'a pas pu être expédié."
          On Error GoTo 0
     End With

End If

Set BinaryStream = nothing
Set RegExp = nothing

Rédigé le : 2006-08-22 16:05:37