EmailLogs.vbs
Post # 214 permalink Topic #213 by mreschke on 2010-11-10 09:57:48 (viewed 251 times)
Table of Contents

This script will email a log file (or any file specified) only if the file is under a certain size. If the file is over that size limit, it will still email, but no log will be attached. See Send Email using VBScript

Usage[-][- -][++]

Script does not use parameters, everything is hard coded, so just call the .vbs. Great in a scheduled task to email you a daily log file. Can easily be modified with parameters for various purposes.

Script[-][- -][++]

EmailLogs.vbs
Option Explicit
Dim iMsg, iConf, Flds, pc    'Email vars
Dim logFile, maxLogSize        'Log vars
Dim FSO, File, strSize        'File size vars

'by mReschke
logFile = "C:\Logs\somelog.log"
maxLogSize = 6    'in MB

Set FSO = CreateObject("Scripting.FileSystemObject")
if FSO.FileExists(logFile) Then
    Set File = FSO.GetFile(logFile)
    strSize = File.Size / 1024 / 1024    'Now in MB
    Call SendLogEmail
End If
 
' ---------------------------------------------------------------------------------
Sub SendLogEmail()
    Set pc = CreateObject("Wscript.Network")
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
 
    Const schema = "http://schemas.microsoft.com/cdo/configuration/"
    dim emailBody
 
    Flds.Item(schema & "sendusing") = 2
    Flds.Item(schema & "smtpserver") = "xxx.xxx.xxx.xxx"
    Flds.Item(schema & "smtpserverport") = 25
    Flds.Item(schema & "smtpauthenticate") = 0
    'Flds.Item(schema & "sendusername") = "domain\username"
    'Flds.Item(schema & "sendpassword") = "pass"
    Flds.Item(schema & "smtpusessl") = 0
    Flds.Update
 
    With iMsg
        .To = "you@mail.com,you2@mail.com"
        .From = "Your Log <yourserver@mail.com>"
 
        if strSize <= maxLogSize Then
            .Subject = pc.ComputerName & " log"
            .AddAttachment(logFile)
            emailBody = "<b>Production Log for " & pc.ComputerName & "</b>"
        Else
            .Subject = pc.ComputerName & " log too big"
            emailBody = "<font color='red'><b>Log file for " & pc.ComputerName & " is greater than " & maxLogSize & "MB.</font> It is " & strSize & "MB</b>"
        End If
 
        .HTMLBody = emailBody
 
        .Sender = "Your Log <yourserver@mail.com>"
        .Organization = "Your Company"
        .ReplyTo = "replyhere@mail.com"
 
        Set .Configuration = iConf
 
        .Send 
    End With   
 
    ' Release Interfaces
    Set iMsg = nothing
    Set iConf = nothing
    Set Flds = nothing
End Sub