MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

Queremos el resultado de un script por email

Bien, pues aquí tenemos un script que podemos lanzar para que nos informe del estado de los servicios por email diariamente.

   1: 'Este script sirve para comprobar cambios de estado de los servicios
   2: ' usando event log y enviar después un aviso por email
   3:  
   4: '(c) Juansa 03-12-2008
   5: 'leer el archivo
   6: Function leearchivo (nombreArchivo)
   7:     Const ForReading = 1, ForWriting = 2
   8:        Dim fso, f
   9:        Set fso = CreateObject("Scripting.FileSystemObject")
  10:     Set f = fso.OpenTextFile(nombreArchivo, ForReading)
  11:     leearchivo = f.ReadAll
  12: End Function
  13: 'existe el archivo
  14: Function Existe(archivo)
  15:    Dim fso
  16:    Set fso = CreateObject("Scripting.FileSystemObject")
  17:    If (fso.FileExists(archivo)) Then 
  18:     Existe = True
  19:    Else
  20:       Existe = False
  21:    End If
  22:    
  23: End Function
  24: 'cuerpo script
  25: 'Creamos constantes y variables
  26: Const ForReading = 1, ForWriting = 2
  27: Dim TabStop, NewLine, filename, strDate, dia, mes, ano
  28: TabStop = Chr(9)
  29: NewLine = Chr(10)
  30: dia = left(Date, 2)
  31: ano = right(Date, 4)
  32: mes = mid(Date, 4,2)
  33: strDate = dia & "-" & mes & "-" & ano
  34: filename = "c:\scripts\InformeServicios_" & strDate & ".txt"
  35:  
  36: 'comprobaremos que el informe no exista ya
  37: if Existe(filename) = true then
  38: 'no se ejecutará nada
  39: else
  40: Dim fso, miarchivo
  41: Set fso = CreateObject("Scripting.FileSystemObject")
  42: set miarchivo = fso.CreateTextFile(filename, True)
  43: miarchivo.close
  44: Set objFSO = CreateObject("Scripting.FileSystemObject")
  45: Set objFile = objFSO.OpenTextFile(filename, ForWriting, True)
  46:  
  47: Set dtmConvertedDate = CreateObject("wbemScripting.SwbemDateTime")
  48:  
  49: StrComputer = "."
  50: Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=Impersonate}!\\" & strComputer &  "\root\cimv2")
  51:  
  52: Set ColServiceEvents = objWMIService.ExecQuery ("SELECT * FROM Win32_NTLogEvent WHERE LogFile =  'System' AND " & "EventCode = '7036'")
  53:  
  54: For Each strEvent in ColServiceEvents
  55:     dtmConvertedDate.Value = strEvent.TimeWritten
  56:     objFile.WriteLine dtmConvertedDate.GetVarDate
  57:     objFile.WriteLine strEvent.Message
  58: Next
  59:  
  60:  
  61: 'el archivo se ha creado ya
  62: 'vamos a enviar el email
  63: 'OJO CON LOS DATOS A RELLENAR
  64: esquema = "http://schemas.microsoft.com/cdo/configuration/"
  65: Set cdoConfig = CreateObject("CDO.Configuration")
  66:     With cdoConfig.Fields 
  67:         .Item(esquema & "sendusing") = 2 
  68:         .Item(esquema & "smtpserver") = "tuservidorSMTP" 
  69:         .Item(esquema & "smtpauthenticate") = 1 
  70:         .Item(esquema & "sendusername") = "TuUsuario"
  71:         .Item(esquema & "sendpassword") = "Tuclave"
  72:         .Item(esquema & "smtpserverport") = 25 'si se usa otro
  73:         .Item(esquema & "smtpusessl") = False 'si se usa ssl True
  74:         .Item(esquema & "smtpconnectiontimeout") = 60
  75:         .update
  76:     End With
  77: Set cdoMessage = CreateObject("CDO.Message")
  78:     With cdoMessage
  79:         Set .Configuration = cdoConfig
  80:         .From = "tucuenta@tudominio"
  81:         .To = "tucuenta@tudominio"
  82:         .Subject = "prueba de envío"
  83:         .TextBody = "El equipo ha tenido un error, revisa el registro"
  84:         .Send
  85:     End With
  86: Set cdoMessage = Nothing
  87: Set cdoConfig = Nothing
  88: end if
  89: ' wscript.Echo "FINAL"

Cualquier otro script puede montarse de la misma manera, así como asignar el envío de un correo a un eventtrigger por ejemplo.


Posted Wed, Dec 3 2008 16:57 by juansa
Filed under: