Tuesday, December 4, 2012

Using Blat Statements in Your Windows Batch Scripts


Blat?

If you look up the word "Blat" on Wikipedia, the first definition that comes up is "a form of corruption in Russia and the Soviet Union".  Strange, but don't worry, that is not what I will be talking about today.

The Blat that I am going to be talking about today is a public domain windows command line utility that sends email using SMTP.  This tool is extremely handy when creating windows batch scripts because it allows you to easily send email updates during your batch process.  Blat must be installed on the server by an administrator and configured with an SMTP email account to work properly.

Where to Get Blat

Blat is available for free from http://www.blat.net.  Since the Blat application is in the public domain, there is no cost and very little limitations associated with it.  Be sure to check the license agreement on their website for more information.

Installation / Configuration (SMTP)

The installation process is pretty straight forward as long as you are comfortable running command line applications, which I am assuming you are, otherwise this blog would be pretty boring.  I am only going to talk about configuring SMTP for Blat at this time.  Blat is capable of POP3 and NNTP (posting to Usenet)

Installation Step 1: Unpacking the zip file
The first thing you need to do is extract the contents from the zip file.  Below I have outlined the folder structure in version 307 32bit.
  • blat307
    • full
      • blat.dll
      • blat.lib
      • blat.exe
      • blatdll.h
    • docs
      • ChangeLog.txt
Extract these files into a permanent directory on the machine in which you run your batch scripts.  I have seen some administrators use c:\windows\system32, but it is probably best to install it to its own directory in "Program Files" to keep the system32 directory clean.

Installation Step 2: SMTP Configuration
For this step you will need to have a dedicated email account set up and the following information on hand.
  • Email address
  • SMTP server name
  • Email User ID
  • Email Password
Open up a command window and navigate to the Blat directory (wherever you created it).  Once there, we need to configure the utility to store the default SMTP information for sending emails from the system.

Below is the syntax for the install switch.

-install[SMTP|NNTP|POP3] <server addr> <sender's email addr> [<try n times> [<port> [<profile> [<username> [<password>]]]]]

The <try n times> and <port> may be replaced by a '-' to use defaults.  The default number of tries is 1 and the default port for SMTP is 25.  The <profile> can also be replaced by a '-' if you plan on specifying a username and password.

Below is an example of the syntax I used to get Blat installed on my local machine.  You may have to modify this syntax unless your email address is me@myemailserver.com.
C:\Program Files\Blat\full>blat -installsmtp smtp.myemailserver.com me@myemailserver.com - - - MyUserName MyPassword

So go ahead and modify that string with your email connection information and execute it.  Once complete, you should get the message below stating a successful configuration.

SMTP server set to smtp.myemailserver.com on port 25 with user me@myemailserver.com, retry 1 time(s)


Please note that when setting up Blat with a default SMTP connection, this connection information is stored in the registry.  This includes the Username and Password.  Don't worry too much though, the password is stored as an encrypted string within a registry key.

Congratulations!  Your Blat utility is now should be configured!  Let's move to the next and final step...

Installation Step 3: Testing
Now that you have successfully configured your Blat utility, lets test it out.  You should still have your command window open to your Blat directory.  If not, do that now.

We are going to test sending an email by using the bare minimum amount of commands as possible.  
The ones we will use are:
  • -to
  • -subject
  • -body
You can probably imagine what my example is going to look like, but I am going to post it anyways...  See below.
C:\Program Files\Blat\full>blat -to scott@whatever_his_email_is.com -subject "Great Job!" -body "Scott, you are doing a great job with your blog.  I will subscribe and read every post!"

After executing this command, you should get a confirmation message from the Blat utility stating the sender and receiver of the email.

Now with this power comes responsibility.  I do not recommend sending your co-workers fake lottery numbers from their "future self" by using the -f switch which will override the default sender's email address making it appear they sent a message to themselves.  That is not nice.

For a full list of switch commands please check out the syntax guide on the Blat website below.
http://www.blat.net/syntax/syntax.html


Using Blat Statements in Your Windows Batch Scripts

Now that you understand the technical aspects of how Blat statements function, lets talk a little about how to use these in your windows batch scripts.  I will write a little about a few different types of ways to use this to your advantage.

Simple Process Updates
Email communications are probably most commonly used as status updates in a process. Whether the process is is at the start, middle or end.  The key thing to having meaningful status updates is to be sure that the subject lines are consistent.  I usually start the subject line with the name of the process that is being run and following it up with a short description of the update.  For example: "AM Actuals Load: Process Started".  The consistency allows you to easily watch different processes that may be running in parallel.

Error Notifications
To me, this is the most important use of email notifications within my scripts.  I don't really have to go into detail why because it pretty much speaks for itself.  I posted some sample code about how to configure batch scripts for MaxL error checking previously.  This code had the following Blat statement in it.
findstr /m "ERROR" C:\LogFiles\log_file_name.log
if %errorlevel%==0 ( blat -t me@email.com -priority 1 -s "AM Backup: Level0 backup failed" -body "Level0 backup failed. Aborting Process." -attacht C:\LogFiles\log_file_name.log
exit )
In this example, I am sending a high priority message to myself with the log file attached to the email message.  I used the -priority switch to specify that the priority of the message will be 1 or High and I used the -attacht to attach the log file directly to the message.  Attaching the log file directly to the failure notification is priceless when trying to turn around a resolution quickly.

That's about it.  Happy emailing!

1 comment: