Need help with VBS for event logs that sends via SMTP.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Ian Lilkendey (ian.lilkendey_at_zisinternet.com)
Date: 11/17/04


Date: 17 Nov 2004 06:33:04 -0800

Hi All,

I am pretty new to VB Scripting. I am looking for a script that
queries several servers event logs and generates an html report with
all warnings and failures. It would also be nice if it e-mailed the
report via SMTP. I have a similar solution in place that checks disk
space. Unfortunately I have been unable to dissect it properly. I
found a couple that claim to accomplish this on google groups, but
they seem to be missing some code(see below). I appreciate anyones
suggestions or help.
Thanks,

Ian Lilkendey

Here is the one I that I cleaned up, that I got off of google. I fixed
a couple of things to get the e-mail to work. As you can see it has
additional functions. I think it is missing some things but i am still
to newb to figure it out. the formatting may get a little strange from
the paste.

'// This script checks the listed servers for: //
'// Disk space. //
'// installed hotfixes. //
'// System Up time //
'// All services set to "Auto" startup are running. //
'// Operating system, current service pack //
'// and available physical memory //
'// Error and Warning events in the Event logs //
'// Gets information about NTbackup //
'// The results are compiled into a htm mail //
'// and e-mailed to the administrator //
'// //
'/////////////////////////////////////////////////////////////////////

'Revision 1.1.4

'//////////////////////////////////////////////////////////////////////
'// //
'// daily_server_HTML_1.1.4.vbs //
'// //
'// Schedule this script to run daily. //
'// //
'//////////////////////////////////////////////////////////////////////

'Configure User defined variables you need to edit the following
section
company = "***"
servers = Array("usctja11")
StrUser = "uscom01\*******"
StrPass = "********"
avsw = "MCA"
'set to NAV to check for Norton AV or anything else for McAfee

'/////////////////////////////////////////////////////////////////////

'System variables - dont change
const HKEY_LOCAL_MACHINE = &H80000002
ForWriting = 2
forReading = 1
forWriting = 2
forAppending = 8

Set fs = WScript.CreateObject("Scripting.FileSystemObject")

if avsw = "NAV" then
strKeyPath = "SOFTWARE\Symantec\SharedDefs"
strValueName = "DEFWATCH_10"
else
strKeyPath = "SOFTWARE\Network Associates\TVD\VirusScan
enterprise\CurrentVersion"
strValueName = "szVirDefDate"
end if

CutOff = Date() - 1
'For McAfee VirusScan Enterprise, set the strKeyPath as: strKeyPath =
"SOFTWARE\Network Associates\TVD\VirusScan enterprise\CurrentVersion"

'/////////////////////////////////////////////////////////////////////
Dim cutoff
Dim Newdate
Dim MyCDONTSMail2
Dim HTML
HTML = "<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML//EN"">"
HTML = HTML & "<html>"
HTML = HTML & "<head>"
HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=0>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=3 width=219>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<img src=http://www.somewhere.com/images/logo.gif
width=219 height=76 alt=A company logo>"
HTML = HTML & "</th>"
HTML = HTML & "<th bgcolor=#639ACE colspan=3>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#000000>Server checks
results for " & company & "</font> </b>"
HTML = HTML & "<br>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#000000> on: " &
date() & " at: " & time () & "</font> </b>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "</table>"
HTML = HTML & "<p>"

For each server in servers
strserver = server
checks(strserver)
Next

'/////////////////////////////////////////////////////////////////////
'FUNCTIONS
'/////////////////////////////////////////////////////////////////////
Function checks (strserver)

'Run the checks
chkdiskspace (strserver)
chkuptime (strserver)
chkopsys (strserver)
chkservices (strserver)

if avsw = "NAV" then
chknav strKeyPath, strValueName
else
chknetass strKeyPath, strValueName
End If

chkevents (strserver)
chkhotfix (strserver)
'End Checks
End Function 'end function checks

'/////////////////////////////////////////////////////////////////////

Sub chkdiskspace (strserver)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strserver & "\root\cimv2")

Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk where DriveType = 3")

HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=3>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=6 width=200>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#FFFFFF>Disk Space
for " & strserver & "</font></b></p>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font face=Tahoma
color=#000080 size=2>Disk ID</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=200><font face=Tahoma
color=#000080 size=2>Percentage Free Space</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font face=Tahoma
color=#000080 size=2>Free Space</font></th>"
HTML = HTML & "</tr>"

For each objDisk in colDisks
HTML = HTML & "<TR><TD><font face=Tahoma color=#000080 size=2> " &
objDisk.Name & "</font></TD>"
if (objDisk.Freespace/objDisk.Size)*100 <=10 then
fontcol = "#FF0000"
else
fontcol = "#000080"
End If
HTML = HTML & "<TD><font face=Tahoma color=" & fontcol & " size=2>" &
(objDisk.Freespace/objDisk.Size)*100 & "</font></TD>"
HTML = HTML & "<TD><font face=Tahoma color=" & fontcol & " size=2>" &
objDisk.Freespace/1024000 & "MB</font></TD></TR>"
Next
End sub 'End function chkdiskspace

'/////////////////////////////////////////////////////////////////////

sub chkuptime (strserver) 'Shows system up time
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strserver & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select LastBootUpTime from Win32_OperatingSystem")

HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=3>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=6 width=400>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#FFFFFF>Server Uptime
for " & strserver & "</font></b></p>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#DEDBDE width=59><font face=Tahoma
color=#000080 size=2>Server Up Since</font></th>"
HTML = HTML & "</tr>"

For Each objOS in colOperatingSystems
HTML = HTML & "<TR><TD><font face=Tahoma color=#000080 size=2>" &
fDateDisplay(objOS.LastBootUpTime) & "</font></TD>"
HTML = HTML & "</TR>"
HTML = HTML & "</table>"

dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)

Next
End sub 'chkuptime (strserver)

'////////////////////////////////////////////////////////////////////

Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
'//////////////////////////////////////////////////////////////////
function fDateDisplay(num)

' Takes a datetime value from WMI and converts it to date and time
format.

dim year
dim month
dim day
dim time
If IsNull(num) Then
fDateDisplay = "N/A"
Exit Function
End If
year = left(num,4)
month = mid(num,5,2)
day = mid(num,7,2)
time = mid(num,9,2) & ":" & mid(num,11,2) & ":" & mid(num,13,2)
fDateDisplay = dateserial(year,month,day) & " " & cdate(time)

end function 'end of system uptime

'/////////////////////////////////////////////////////////////////////

Function chkopsys (strserver)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strserver & "\root\cimv2")

Set colopsys = objWMIService.ExecQuery _
("Select Caption, CSDVersion, FreePhysicalMemory from
Win32_OperatingSystem")

HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=3>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=6 width=400>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#FFFFFF>Operating
System Info For " & strserver & "</font></b></p>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#DEDBDE width=59><font face=Tahoma
color=#000080 size=2>Operating System</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font face=Tahoma
color=#000080 size=2>Service Pack</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font face=Tahoma
color=#000080 size=2>Free Memory</font></th>"
HTML = HTML & "</tr>"

For each objopsys in colopsys
HTML = HTML & "<TR><TD><font face=Tahoma color=#000080 size=2>" &
objopsys.Caption & "</font></TD>"
HTML = HTML & "<TD><font face=Tahoma color=#000080 size=2>" &
objopsys.CSDVersion & "</font></TD>"
HTML = HTML & "<TD><font face=Tahoma color=#000080 size=2>" &
objopsys.FreePhysicalMemory & "K</font></TD></TR>"
Next

End Function 'End function chkopsys

'/////////////////////////////////////////////////////////////////////

Function chkservices (strserver)

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strserver & "\root\cimv2")

Set colsvc = objWMIService.ExecQuery _
("Select * from Win32_Service Where StartMode = 'Auto'")

HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=3>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=6 width=400>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<b><font face=Tahoma size=3 color=#FFFFFF>Services not
running on " & strserver & "</font></b></p>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#DEDBDE width=59><font face=Tahoma
color=#000080 size=2>Service Name</font></th>"
HTML = HTML & "</tr>"

For each objsvc in colsvc
if objsvc.State = "Stopped" Then
serviceerr = "failed"
HTML = HTML & "<TR><TD><font face=Tahoma color=#FF0000 size=2>" &
objsvc.DisplayName & "</font></TD>"
else
End If
Next

if serviceerr = "failed" then
else
HTML = HTML & "<TR><TD><font face=Tahoma color=#000080 size=2>All
Services are running</font></TD>"
End If
HTML = HTML & "</table>"
End Function 'End function chkservices

'/////////////////////////////////////////////////////////////////////

Sub chknav (strKeyPath, strValueName)
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strserver & "\root\default:StdRegProv")
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

HTML = HTML & ("<table BORDER=0 width=100% cellspacing=0
cellpadding=3>")
HTML = HTML & ("<tr>")
HTML = HTML & ("<th bgcolor=#639ACE colspan=6>")
HTML = HTML & ("<p align=left>")
HTML = HTML & ("<b><font face=Tahoma size=3 color=#FFFFFF>Norton
Anti-Virus definition file Version</font></b></p>")
HTML = HTML & ("</th>")
HTML = HTML & ("</tr>")
HTML = HTML & ("<TR><TD><font face=Tahoma color=#FFFFFF size=1> " &
strValue & "</font></TD></TR>")
HTML = HTML & ("</table>")

End Sub ' End function chknav
'///////////////////////////////////////////////////////////////////////
Sub chknetass (strKeyPath, strValueName)
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strserver & "\root\default:StdRegProv")
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

HTML = HTML & ("<table BORDER=0 width=100% cellspacing=0
cellpadding=3>")
HTML = HTML & ("<tr>")
HTML = HTML & ("<th bgcolor=#639ACE colspan=6>")
HTML = HTML & ("<p align=left>")
HTML = HTML & ("<b><font face=Tahoma size=3 color=#FFFFFF>McAfee
Anti-Virus definition file Version</font></b></p>")
HTML = HTML & ("</th>")
HTML = HTML & ("</tr>")
HTML = HTML & ("<TR><TD><font face=Tahoma color=#FFFFFF size=1> " &
strValue & "</font></TD></TR>")
HTML = HTML & ("</table>")

End Sub 'End Function chknetass
'/////////////////////////////////////////////////////////////////////
Function chkevents (strserver)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Security)}!\\" & _
strserver & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent where Type = 'error' or Type =
'warning'")

HTML = HTML & ("<table BORDER=0 width=100% cellspacing=0
cellpadding=3>")
HTML = HTML & ("<tr>")
HTML = HTML & ("<th bgcolor=#639ACE colspan=6 width=200>")
HTML = HTML & ("<p align=left>")
HTML = HTML & ("<b><font face=Tahoma size=3 color=#FFFFFF>Error and
Warning events for the last 24Hrs on " & strserver &
"</font></b></p>")
HTML = HTML & ("</th>")
HTML = HTML & ("</tr>")
HTML = HTML & ("<tr>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=60><font face=Tahoma
color=#000080 size=1>Computer Name</font></th>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=200><font face=Tahoma
color=#000080 size=1>Event Code/Log</font></th>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=60><font face=Tahoma
color=#000080 size=1>Source Name</font></th>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=60><font face=Tahoma
color=#000080 size=1>Time Written</font></th>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=60><font face=Tahoma
color=#000080 size=1>Type</font></th>")
HTML = HTML & ("<th bgcolor=#C0C0C0 width=60><font face=Tahoma
color=#000080 size=1>Message</font></th>")
HTML = HTML & ("</tr>")

For Each objEvent in colLoggedEvents
if WMIDateStringToDate(objEvent.TimeGenerated) >= CutOff then
HTML = HTML & ("<TR>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
objEvent.ComputerName & "</font></TD>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
objEvent.EventCode & " / " & objEvent.LogFile &"</font></TD>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
objEvent.SourceName & "</font></TD>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
WMIDateStringToDate(objEvent.TimeGenerated) & "</font></TD>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
objEvent.Type & "</font></TD>")
HTML = HTML & ("<TD><font face=Tahoma color=#000080 size=1> " &
objEvent.Message & "</font></TD>")
HTML = HTML & ("</TR>")
Else
End If
Next

End Function 'End function chkevents
'//////////////////////////////////////////////////////////////////////////////////////
Function chkhotfix (strserver)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strserver & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery _
("Select * from Win32_QuickFixEngineering")
HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=3>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#639ACE colspan=6 width=400>"
HTML = HTML & "<p align=left>"
HTML = HTML & "<b><font face=Tahoma size=3
color=#FFFFFF>Hotfixinstallations on " & strserver & "</font></b></p>"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#DEDBDE width=59><font
face=Tahomacolor=#000080 size=2>Description</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font
face=Tahomacolor=#000080 size=2>Hot Fix ID</font></th>"
HTML = HTML & "<th bgcolor=#DEDBDE width=60><font
face=Tahomacolor=#000080 size=2>Installation By</font></th>"
HTML = HTML & "</tr>"
For Each objQuickFix in colQuickFixes
HTML = HTML & "<TR><TD><font face=Tahoma color=#000080 size=2>"
&objQuickFix.Description & "</font></TD>"
HTML = HTML & "<TD><font face=Tahoma color=#000080 size=2>"
&objQuickFix.HotFixID & "</font></TD>"
HTML = HTML & "<TD><font face=Tahoma color=#000080 size=2>"
&objQuickFix.InstalledBy & "K</font></TD></TR>"
Next
'////////////////////////////////////////////////////////////////////////////
' Html footer
'///////////////////////////////////////////////////////////////////////////
HTML = HTML & "<table BORDER=0 width=100% cellspacing=0
cellpadding=0>"
HTML = HTML & "<tr>"
HTML = HTML & "<th bgcolor=#ffffff colspan=6>"
HTML = HTML & "<p align=center>"
HTML = HTML & "<font face=Tahoma color=#000000 size=1>Output generated
by <a href=mailto:ian.lilkendey@zisinternet.com> Ian"
HTML = HTML & "</th>"
HTML = HTML & "</tr>"
HTML = HTML & "</table>"
HTML = HTML & "</body>"
HTML = HTML & "</html>"End function 'chkhotfix (strserver)'End
function chkhotfix
'/////////////////////////////////////////////////////////////////////

Set objErrMail = CreateObject("CDO.Message")
objErrMail.From = "ian.lilkendey@zisinternet.com"
objErrMail.To = "ian.lilkendey@zisinternet.com"
objErrMail.Subject = "Server checks for " & company & " on: " & date()
objErrMail.HTMLBody = CStr("" & HTML)
'objErrMail.HTMLBody = CStr("" & strBody)
objErrMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
= 2
objErrMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "172.18.174.222"
objErrMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25
objErrMail.Configuration.Fields.Update
objErrMail.Send
'/////////////////////////////////////////////////////////////////////
WScript.echo ("Complete")
'~~[/script]~~



Relevant Pages

  • Re: Event logs last 24 hours.
    ... set the strKeyPath as: ... Dim Newdate ... Function checks (strserver) ... End sub 'End function chkdiskspace ...
    (microsoft.public.scripting.vbscript)
  • Need help with VBS that sends event log errors via SMTP.
    ... servers = Array ... Dim Newdate ... Function checks (strserver) ... End sub 'End function chkdiskspace ...
    (microsoft.public.scripting.vbscript)
  • Re: Array and console.writeline
    ... "a,b,c,d,e,f" are name of servers. ... I need to run Sub for every server from ... Dim tablica() As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: WMI Execute failure
    ... >>> script failed with a returned result of 3. ... I receive the same error dialogue. ... Dim strServer ... Dim strNameSpace ...
    (microsoft.public.scripting.wsh)
  • Re: Got Svr, need DBs
    ... >I have a bunch of servers, ... Private Sub FillServerList() ... Dim oNames As SQLDMO.NameList: Set oNames = oSQLApp.ListAvailableSQLServers ... Dim lngN As Long ...
    (microsoft.public.vb.general.discussion)