Solved

VB script to email performance alerts

Posted on 2007-11-16
8
2,297 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
  • 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 500 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now