How to add email alerts and logging in vbscript

I have a vb script that zips files and then delete them. I wish to add two things to it.
1) Email alert (the script should send an email to multiple people when it finishes execution)
2) Enable logging (the script should log events like the time it started to execute, the server name on which it is running, the files it has zipped and deleted and any errors that it encountered while zipping and deleting)

Please help
ghelaniabhishekAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, for emailing, you can use this code, and just duplicate the
SendEmail strServer, strTo, strFrom, strSubject, strBody

line for multiple strTo addresses.

For logging, create a text by using
strLogFile = "log.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLogFile, True)

and then write the current time with
objLog.WriteLine "Script started: " & Now

and continue to output to the log file in that fashion.

At the end of the code, close the log file with
objLog.Close

Regards,

Rob.
' Email variables:
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"
strSubject = "Subject Here"
strBody = "This is the body:" & VbCrLf

SendEmail strServer, strTo, strFrom, strSubject, strBody

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
 
        '==This section provides the configuration information for the remote SMTP server.
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
        'Server port (typically 25)
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
        objMessage.Configuration.Fields.Update
        '==End remote SMTP server configuration section==
 
        objMessage.Send
        Set objMessage = Nothing
End Sub

Open in new window

Justin EllenbeckerIT DirectorCommented:
Don't forget with CDO you need the CDOEXM from the exchange management tools on your workstation.  This is a free download from Microsoft's website.  I am working on the original script you had from the previous question as well should have something soon.
Justin EllenbeckerIT DirectorCommented:
Ok here is the script with a results file being written out you have the other script so you are familiar with what lines to change for yoru data.  Tested everything looked good on my end let me know.
On Error Resume Next
Const FOF_CREATEPROGRESSDLG = &H0&
MyZip = "C:\vbjunk\FilePrefix " & Replace(Date,"/",".") & ".zip"
Dir1 = "C:\vbjunk\"

'-------------- create empty zip file ---------
CreateObject("Scripting.FileSystemObject") _
.CreateTextFile(MyZip, True) _
.Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
'-------------- zip ---------------------------

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder(Dir1)
Set resultsfile = oFS.OpenTextFile("C:\vbjunk\resultsfile.txt",2,True)
msgbdy = ""
resultsfile.WriteLine "Script Started " & Now
msgbdy = msgbdy & "Script Started " & Now & VbCrLf
Err.Clear
For Each File In oFolder.Files
	If Right(LCase(file.path),4) <> ".zip" Then
		If File.DateLastModified < Date - 8 Then
			With CreateObject("Shell.Application")
				.NameSpace(MyZip).CopyHere File.path, FOF_CREATEPROGRESSDLG
			End With
			If Err.Number = 0 Then
				resultsfile.WriteLine "File: " & file.path & " addedd successfully"
				msgbdy = msgbdy & "File: " & file.path & " addedd successfully" & VbCrLf
			Else
				resultsfile.WriteLine "File: " & file.path & " failed"
				msgbdy = msgbdy & "File: " & file.path & " failed" & VbCrLf
			End If
			Err.Clear
			wScript.Sleep 15000
			file.delete
		End If
	End If
Next
msgbdy = msgbdy & "Completed: " & Now
resultsfile.WriteLine "Completed: " & Now

strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com; address2@abc.com"
strFrom = "john.doe@abc.com"
strSubject = "Subject Here"
Set objMessage = CreateObject("CDO.Message")


objMessage.To = strTo
objMessage.From = strFrom
objMessage.Subject = strSubject
objMessage.TextBody = msgbdy
 
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ghelaniabhishekAuthor Commented:
Thanks for the script.
Btw, I want to attach the log file we generate to be sent as attachment via the email we send.
Can we do this?
ghelaniabhishekAuthor Commented:
I mean, I want to attach resultsfile.txt to the email sent by the script.
Justin EllenbeckerIT DirectorCommented:
Here is all you need to do.

objMessage.AddAttachment "C:\vbjunk\resultsfile.txt"

Just add that before line 61
ghelaniabhishekAuthor Commented:
Thanks. I'll test it and update you when done. A little occupied today, so might check it on Monday.
RobSampsonCommented:
Hi, did you have a change to test this?

Regards,

Rob.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.