Usuário com melhor resposta
Envio de log por email

Pergunta
-
Pessoal, tenho o script abaixo e funciona, só que eu gostaria que ele me enviasse o log mais recente, só que ele me envia apenas o primeiro log no caso antigo.
'##################################### script envio de log backup###############################
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '8019' ")For Each objEvent in colLoggedEvents
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfNext
wscript.echo body
'##### ENVIA EMAIL#####
Set objEmail = CreateObject("CDO.Message")
objEmail.From = xxxx@xxx.com.br
objEmail.Subject = "Backup"
objEmail.To = xxxx@xxx.com.br
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send'#############################################FIM#####################################
Obrigado!!!!
Respostas
Todas as Respostas
-
-
-
-
Fabio, tenta desse jeito aqui.
, eu usava dessa forma para monitorar o event viewer e funcionava que era uma beleza
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '8019' ")For Each Newlog in colLoggedEvents
If Newlog.timewritten > UltLogon then
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfend if
next
wscript.echo body
'##### ENVIA EMAIL#####
Set objEmail = CreateObject("CDO.Message")
objEmail.From = xxxx@xxx.com.br
objEmail.Subject = "Backup"
objEmail.To = xxxx@xxx.com.br
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send'#############################################FIM#####################################
-
-
Laerte, corrigi a mensagem de erro adcionando a linha que esta em negrito no script, só que ele me enviou todos os logs 8019, e não só o ultimo, sabe o que pode ser??
Grato.
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '8019' ")For Each objEvent in colLoggedEvents
For Each Newlog in colLoggedEvents
If Newlog.timewritten > UltLogon then
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfend if
next
wscript.echo body
'##### ENVIA EMAIL#####
Set objEmail = CreateObject("CDO.Message")
objEmail.From = xxxx@xxx.com.br
objEmail.Subject = "Backup"
objEmail.To = xxxx@xxx.com.br
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send'#############################################FIM#####################################
-
Fabio desculpa, já vi o erro aqui, só altera isso aqui no seu script que vai funcionar
If Newlog.timewritten > UltLogon then
Body = ""
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfend if
-
Laerte, desculpe o enchimento de saco, mas ainda apresentou mensagem de erro. segue o script abaixo.
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '8019'")For Each objEvent in colLoggedEvents
For Each Newlog in colLoggedEvents
If Newlog.timewritten > UltLogon thenBody = ""
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfend if
next
wscript.echo body
'##### ENVIA EMAIL#####
Set objEmail = CreateObject("CDO.Message")objEmail.From = xxx@xxx.com
objEmail.Subject = "log"
objEmail.To = aaa@aaa.com
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send -
Fabio tenta agora, desculpe, tinha editado o script errado
, é que estou sem poder testar onde estou agora
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '8019'")For Each objEvent in colLoggedEvents
If objEvent.timewritten > UltLogon thenBody = ""
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlfend if
next
wscript.echo body
'##### ENVIA EMAIL#####
Set objEmail = CreateObject("CDO.Message")objEmail.From = xxx@xxx.com
objEmail.Subject = "log"
objEmail.To = aaa@aaa.com
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send -
-
Fabio,a gora testado e funcionando
, testei aqui e funcionou
, tava faltando repreencher a variavel UltLogon
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '1525'")
For Each objEvent in colLoggedEvents
If objEvent.timewritten > UltLogon thenBody = ""
Body = "Computer Name: " & objEvent.ComputerName & vbcrlf
Body = Body & "Event Code: " & objEvent.EventCode & vbcrlf
Body = Body & "Event Type: " & objEvent.Type & vbcrlf
Body = Body & "Time Written: " & objEvent.TimeWritten & vbcrlf
Body = Body & "Message: " & objEvent.Message & vbcrlf
UltLogon = objEvent.timewrittenend if
next
wscript.echo body
Set objEmail = CreateObject("CDO.Message")objEmail.From = xxx@xxx.com
objEmail.Subject = "log"
objEmail.To = aaa@aaa.com
objEmail.Textbody = Body
objEmail.Configuration.Fields.Update
objEmail.Send -
-
-
Laerte, gostaria de sua ajuda caso tenha tempo:
Preciso de um log que apenas verifique diariamente a pasta Sistema para procurar pelo evento 6008 que é de desligamento incorreto e que fosse enviado para o meu email.
Sou totalmente leigo em vbscript e se você puder me ajudar ficarei eternamente grato.
Abraços
Limajr
-
Adalberto segue script, testa ae
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
data = right("00" & datepart("d",now()),2) & right("00" & datepart("m",now()),2) & datepart("yyyy",now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
& "EventCode = '6008'")
For Each objEvent in colLoggedEventsdatalog = mid(Newlog.timewritten, 7,2) & mid(Newlog.timewritten, 5,2) & mid(Newlog.timewritten, 1,4)
If datalog = data thenBody = Body & "Message: " & objEvent.Message & vbcrlf
end ifnext
wscript.echo body
Set objEmail = CreateObject("CDO.Message")
objEmail.From = remetente
objEmail.Subject = "log"
objEmail.To = destinatarioobjEmail.Textbody = Body
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"seu_servidor_de_smtp"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Configuration.Fields.Update
objEmail.Send -
Laerte,
No windows XP o script funcionou perfeitamente sem erros, mas no 2003 server ele tá dando erro:
Script: c:\log.vbs
Linha: 21
Caract: 1
Erro: Objeto Necessário: 'Newlog'
Código: 800A01A8
Origem: Erro de tempo de execução Microsoft VBscript
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
data = right("00" & datepart("d",now()),2) & right("00" & datepart("m",now()),2) & datepart("yyyy",now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
& "EventCode = '6008'")
For Each objEvent in colLoggedEventsdatalog = mid(Newlog.timewritten, 7,2) & mid(Newlog.timewritten, 5,2) & mid(Newlog.timewritten, 1,4)
If datalog = data then
Body = Body & "Message: " & objEvent.Message & vbcrlf
end ifnext
wscript.echo body
Set objEmail = CreateObject("CDO.Message")objEmail.From = "limajr@hardservice.com.br"
objEmail.Subject = "log"
objEmail.To = "limajr@hardservice.com.br"objEmail.Textbody = Body
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hardservice.com.br"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Configuration.Fields.Update
objEmail.SendAbraços
Adalberto
-
Adalberto, está concertado o erro
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
data = right("00" & datepart("d",now()),2) & right("00" & datepart("m",now()),2) & datepart("yyyy",now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
& "EventCode = '6008'")
For Each Newlog in colLoggedEventsdatalog = mid(Newlog.timewritten, 7,2) & mid(Newlog.timewritten, 5,2) & mid(Newlog.timewritten, 1,4)
If datalog = data then
Body = Body & "Message: " & Newlog.Message & vbcrlf
end ifnext
Set objEmail = CreateObject("CDO.Message")objEmail.From = "limajr@hardservice.com.br"
objEmail.Subject = "log"
objEmail.To = "limajr@hardservice.com.br"objEmail.Textbody = Body
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hardservice.com.br"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Configuration.Fields.Update
objEmail.Send -
-
Pronto Adalberto só envia se tiver alguma coisa registrada no evento
só não esqueça se a resposta lhe ajudou não esqueça de marca-la
strComputer = "."
Body = ""
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEmail = CreateObject("CDO.Message")UltLogon = ""
'### Coleta informacoes do event viewer ###
data = right("00" & datepart("d",now()),2) & right("00" & datepart("m",now()),2) & datepart("yyyy",now())
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System' and " _
& "EventCode = '6008'")
For Each Newlog in colLoggedEventsdatalog = mid(Newlog.timewritten, 7,2) & mid(Newlog.timewritten, 5,2) & mid(Newlog.timewritten, 1,4)
If datalog = data then
Body = Body & "Message: " & Newlog.Message & vbcrlf
end ifnext
If Body <> "" then
Set objEmail = CreateObject("CDO.Message")objEmail.From = "limajr@hardservice.com.br"
objEmail.Subject = "log"
objEmail.To = "limajr@hardservice.com.br"objEmail.Textbody = Body
objEmail.Configuration.Fields.Item _
end if
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hardservice.com.br"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Configuration.Fields.Update
objEmail.Send