Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB script to email performance alerts

Posted on 2007-11-16
8
Medium Priority
?
2,310 Views
Last Modified: 2012-08-14
I set up a performance alert in Windows 2003 SP1 to monitor logical drive space. I'm looking for a way to have the alert when triggered to email me. I did a quick google search and found this site - http://www.windowsitpro.com/Articles/ArticleID/24594/24594.html?Ad=1.

I downloaded the code, changed the values it describes in the article and tried to run it with no success. I get "Unexpected XML declaration - declaration must be at the beginning of the file".
So i remove the first line so it's now <?xml version="1.0"?>. now i get this error " The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available."

here is the full code, maybe someone can tell me if there is an obvious error in the code. I don't know VB at all.

<?xml version="1.0"?>

<job id="SendAlert">
<reference object="CDO.Message"/>
<script language="VBScript">

<![CDATA[
Option Explicit

Dim objArgs, strMsgBody, iMsg, iConf, Flds

Set objArgs = WScript.Arguments
If objArgs.Count > 0 Then
   strMsgBody = objArgs(0)
End If

Set iMsg = CreateObject("CDO.Message")
Set iConf = iMsg.Configuration
Set Flds = iConf.Fields


Flds(cdoSendUsingMethod) = cdoSendUsingPort
Flds(cdoSMTPServer) = "10.10.1.4"
Flds(cdoSMTPServerPort) = 25
Flds(cdoSMTPConnectionTimeout) = 10
Flds(cdoSMTPAuthenticate) = cdoBasic
Flds(cdoSendUserName) = ""
Flds(cdoSendPassword) = ""
Flds.Update

With iMsg
  Set .Configuration = iConf
      .To = "admin@domain.com"
      .From = "alert@domain.com"
      .Subject = "Windows 2000 Performance Alert"
      .TextBody = strMsgBody
      .Send
End With

WScript.Echo "Send mail completed."
WScript.Quit(0)
]]>
</script>
</job>
0
Comment
Question by:js479
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:ExTxCx
ID: 20300286
I have been using the following function for some time now without issue.
Function sendMail()
	Dim objMessage
	'WScript.Echo logPath & "\backup" & strDate & ".html"
	Set objMessage = CreateObject("CDO.Message")
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'use '1' for local SMTP
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmtpServer
	objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSmtpPort
	If strSmtpAuth = "yes" Then
		objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'use '2' for NTLM authentication
		objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strSmtpUser
		objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strSmtpPass
	End If
	If strSmtpSsl = "yes" Then
		objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
	End If
	objMessage.Configuration.Fields.Update
	objMessage.Subject = "FUSD Backup Report for " & strDate & "."
	objMessage.From = strSendingEmail
	objMessage.To = strReportEmail
	objMessage.HTMLBody = IE.Document.Body.InnerHTML
	'objMessage.AddAttachment = logPath & "\backup" & strDate & ".html"
	objMessage.Send
End Function

Open in new window

0
 
LVL 1

Author Comment

by:js479
ID: 20300564
You'll have to forgive me, i don't know programming at all. Should I copy that code into a note pad and name it .wsf like I did with my original script?
And where would I enter the SMTP address and email fields?
0
 
LVL 5

Accepted Solution

by:
JohnDemerjian earned 2000 total points
ID: 20300679
i think this script is self explanatory.  call it "email_me.vbs" and run it from a command prompt or in a batch file with the command  

cscript email_me.vbs

good luck
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "This is the subject of the email" 
objMessage.Sender = "your_name@company.com" 
objMessage.To = "john_jones@company.com, joe.smith@company.com"
objMessage.TextBody = "The network is down, but I'm feeling better."
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
 
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") = "mail.company.com"
 
'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

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 1

Author Comment

by:js479
ID: 20301626
How can I troubleshot this?
I ran it, got no errors but also got no email.
0
 
LVL 3

Expert Comment

by:ExTxCx
ID: 20302172
Check that you don't have a spam filter that is catching these.  I have, in most cases, had to add the sending address to a whitelist in order for spam filters not to hit on them.  Make sure that the ip address of your machine is allowed to relay through the smtp server.
0
 
LVL 5

Expert Comment

by:JohnDemerjian
ID: 20311412
I have also run into the need to add the ip of the machine trying to send the email to the relay list of the smtp server.  
0
 
LVL 1

Author Comment

by:js479
ID: 20313001
Alright - I messed up. I forgot to add the last line of the code you showed me JohnDemerjian.

So I just copied it in and now I get an error.
*****************************************
Windows Script Host
Script:     C:\emailme2.vbs
Line:     25
Char:     1
Error:     The transport failed to connect to the server.

Code:     80040213
Source:     CDO.Message.1
*****************************************



Line 25 is the last line "objMessage.Send"
0
 
LVL 1

Author Comment

by:js479
ID: 20313167
Nevermind - monday morning stupidity. It's working - thank you very much.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question