Script Email Alerts with CDO for Win2K

Use automated email notification to monitor the health of your servers and the completion status of scheduled tasks.

Readers

April 9, 2002

2 Min Read
ITPro Today logo in a gray background | ITPro Today


Automated email notification is a valuable tool for monitoring the health of servers and the completion status of scheduled tasks. I often use Collaboration Data Objects (CDO) for Windows 2000 (cdosys.dll) to add messaging capabilities to administrative scripts. CDO for Win2K uses SMTP to send messages and doesn't require mail client software.

I wrote the Sendalert.wsf script to email Win2K performance alerts. This script uses the Windows Script (.wsf) file format. Sendalert.wsf accepts a command-line argument that becomes the message body. One advantage of the .wsf format is that it lets you reference external type libraries. Sendalert.wsf references the CDO for Win2K library so that the script can use constants defined in the library. The script finds the command-line argument to use for the message text, then creates instances of the Message and Configuration COM classes. The Message object represents a complete email message, whereas the Configuration object references an ActiveX Data Objects (ADO) Fields collection, which stores messaging configuration information such as SMTP settings. The Sendalert.wsf script's configuration fields specify that messages will be sent through an SMTP service hosted by a computer with the DNS name of smtpserver.mycompany.com. The script provides username and password fields for basic (i.e., clear-text) authentication with the SMTP server, but anonymous (i.e., no authentication) is the default if you don't set a value for the SMTPAuthenticate field. Finally, the script sends the message.

Before you use the script, locate the code that callout A in Listing 1 shows. Replace smtpserver.mycompany.com with your SMTP server's DNS name or IP address. In the code that callout B shows, replace [email protected] with the email address to which you want to send the message and replace [email protected] with an email address that you want to display in the message's From field. Then, save the file with a .wsf extension (e.g., C:scriptssendalert.wsf). To test the script, double-click it; you should receive a blank email message. (You can download the entire Sendalert.wsf script from the Windows Scripting Solutions Web site.)

Next, create a .cmd file that contains the line

%systemroot%system32cscript.exe /B C:scriptssendalert.wsf %1

Save this file in the folder that contains Sendalert.wsf. This command launches Sendalert.wsf and passes it a command-line argument.

You use the Microsoft Management Console (MMC) Performance Logs and Alerts snap-in to create a performance alert. On your performance alert's Action tab, select the Run This Program check box and specify the path to the .cmd file (e.g., C:scriptssendalert.cmd). Finally, click Command Line Arguments. Select the Single Argument String check box, then select the check boxes next to the items that you want to display in the message body. When the alert is triggered, the system will send you an email notification.

—Chris Toney
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like