Waiting and watching STSADM run on the CMD line is an exercise in patience. Lately I’ve been incorporating two techniques to email myself updates for the freedom to walk away from the keyboard.
A quick command line EXE project allows me to send email by giving parameters (number 0-5). If 5 is blank, no big deal just send a plain email with subject and body. If 5 exists then attempt to open that text file, read content, and send as email body.
That’s great, but … how does this help me? Great question!!
1) Send email at key script milestones
The freedom to walk away and receive alerts as progress continues allows you to be stay confident and know progress is moving forward. Just add “SendMail.exe” inline with your existing CMD files to send an email at that step in the process.
2) Send email with script outcome
The detailed knowledge of what return value came from the CMD when it finished is helpful. Imagine running a site move (STSADM backup/delete/restore) and heading out for dinner. It’s very nice to get an email showing “Operation completed successfully” three times.
Just use “SendMail.exe” with parameter 5 being the text file full path and name.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;
namespace SendMail
{
class Program
{
static void Main(string[] args)
{
//0-from
//1-to
//2-subject
//3-body
//4-server
//5-filename
string textFile = "";
if (!String.IsNullOrEmpty(args[5]))
{
//send text file as body
TextReader tr = new StreamReader(args[5]);
textFile = tr.ReadToEnd();
tr.Close();
}
//create message
Console.WriteLine("from {0} to {1} subject {2} body {3} on server {4}", args[0], args[1], args[2], args[3], args[4]);
MailMessage message = new MailMessage(args[0], args[1], args[2], args[3]);
//if file was given modify body
if (textFile != "") message = new MailMessage(args[0], args[1], args[2], textFile);
SmtpClient emailClient = new SmtpClient(args[4]);
emailClient.Send(message);
}
}
}
Recent Comments