Thursday, December 18, 2008

Vbscript Send Email With Cdomessage

Writen by Shaun Vermaak

Some my feel that apart from it being a nifty trick, there isn't any real practical use for sending E-Mail from a script but lets imagine the following scenario:

You have a business critical server called "ImpServ01" that must be available at all costs. The following script can be setup to run every five minutes to check the availability of the server and email you if the server becomes unavailable. This example uses a SMTP server called "SMTP.YourDomain.com" and sends mail from "ImpServ01@YourDomain.com" to "Admin@YourDomain.com".

Option Explicit

Dim strComputer Dim colPingStatus Dim objPingStatus Dim objMessage

strComputer = "ImpSrv01"

Set colPingStatus = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus WHERE address = '" & strComputer & "'")

For Each objPingStatus In colPingStatus

If IsNull(objPingStatus.StatusCode) or objPingStatus.StatusCode <> 0 Then

Set objMessage = CreateObject("CDO.Message")

objMessage.From = strComputer & "@YourDomain.com"

objMessage.To = "Admin@YourDomain.com"

objMessage.Subject = strComputer & " is unavailable"

objMessage.Textbody = strComputer & " is no longer accessible over the network."

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP.YourDomain.com"

objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMessage.Configuration.Fields.Update

objMessage.Send

End If Next

Set colPingStatus = Nothing Set objMessage = Nothing

To attach a file to the E-Mail add the following line:

objMessage.AddAttachment "SomeDocument.doc"

Shaun Vermaak - www.ITtelligence.co.za

No comments: