Hola Amigos:
Tengo el siguiente problema con este script.
me funciona la parte de buscar y guardar un evento, pero cuando no se encuentran eventos deberia guardar en el archivo la leyenda "No se encontraron eventos." pero no lo hace, que estoy haciendo mail?
muchas Gracias!!! :laugh:
'Busca el nombre de la Pc local, de aca sacamos los eventos
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputer = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'conectamos
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
& strComputer & "\root\cimv2")
' definimos que buscar
Set colEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where LogFile='Security' AND (EventCode=528 or EventCode=540)")
'creamos el archivo que contendra el log filtrado
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\backup_evt\" & Day(Date) & "-" & Month(Date) & "-" & Year(Date) & ".cvs")
'le decimos que mostrar y en que orden
For Each objEvent in colEvents
strTimeWritten = objEvent.TimeWritten
dtmTimeWritten = CDate(Mid(strTimeWritten, 5, 2) & "/" & _
Mid(strTimeWritten, 7, 2) & "/" & Left(strTimeWritten, 4) _
& " " & Mid (strTimeWritten, 9, 2) & ":" & _
Mid(strTimeWritten, 11, 2) & ":" & Mid(strTimeWritten, 13, 2))
dtmDate = FormatDateTime(dtmTimeWritten, vbShortDate)
dtmTime = FormatDateTime(dtmTimeWritten, vbLongTime)
strEvent = dtmDate & vbTab
strEvent = strEvent & dtmTime & vbTab
strEvent = strEvent & objEvent.SourceName & vbTab 'nombre
strEvent = strEvent & objEvent.Type & vbTab 'Error
strEvent = strEvent & objEvent.Category & vbTab 'categoria
strEvent = strEvent & objEvent.EventCode & vbTab 'codigo
strEvent = strEvent & objEvent.User & vbTab 'usuario
strEvent = strEvent & objEvent.ComputerName & vbTab 'maquina
'mensaje que avisa que no se encontraron eventos no funca
strDescription = objEvent.Message
If IsNull(strDescription) Then
strDescription = "No se encontraron eventos."
End If
strDescription = Replace(strDescription, vbCrLf, " ")
strEvent = strEvent & strDescription
objFile.WriteLine strEvent
Next
objFile.Close
El problema es que estas verificando si no hay eventos dentro del For Each, pero si no hay eventos ese For Each nunca se da, por lo cual nunca llegas a escribir nada :P
Saludos
Muchas Gracias Por tu pronta respuesta. como podria hacer esta verificacion. soy nuevo en esto y hace dias que me estoy rompiendo la cabeza!!!
mil gracias!!!
Antes de entrar al for each, incluye un if colEvents.count=0 :P
Saludos
:laugh: ;-) ;-) ;-) ;-) ;-)
Muchisimas Gracias!!!
me acabas de resolver un problema enorme, ya tenia quemada la cabeza!!!