Methodologically Sending Messages

Long, long ago, in a blog post not too far away, I wrote about how to use CDO.Message to send email messages via Windows PowerShell. In an Exchange Server 2007 class, the topic came up again and I ended up briefly demonstrating the basics of composing the to, from, subject, and body of a message.

Certainly, putting this sort of functionality into a script or function that can be quickly and easily used has immense power… and danger. But this is true of scripting in general. The whole idea of writing scripts and functions is that they make it incredibly easy to do something which you might need to do often, or even things you do rarely but don’t want the hassle of remember all of the little incantations which make up a useful operation.

Whether you need to send messages for systems management reporting, or as a part of an application or workflow you’re automating, having a quick functional technique for sending email is indispensable. Therefore, I thought it was worth revisiting using CDO.Message’s Send method to send email. Whether you’re using PowerShell version 1.0, PowerShell 2.0, and using XP, Vista, Windows 7, or servers running Windows Server 2003, 2008, 2008 R2, with or without Exchange locally, if you have CDO.Message installed, for example with Outlook or Exchange, this tiny script should work.

param(

$to = “coolpeople@gk.com

$from = “administrator@acme.com

$subject = “This is a test”

)

$o = new-object -com CDO.Message

$o.To = $to

$o.From = $from

$o.Subject = $subject

$o.TextBody = $input

$o.Send()

While this script could have easily been implemented as a function, the important thing is not its implementation, but the ease of use. Let’s look at some quick examples. Note that the body of the email is formulated from the input to the script, while the to, from, and subject values are passed as parameters. Our first example uses the default values in the script’s parameter (param) block for the to, from, and subject fields of the message.

“Hello, this is a scripted message” | ./send

This sends the message to the default recipient coolpeople@gk.com, from administrator@acme.com, with the subject of “This is a test.” Of course, the body of the message is simply the string “Hello, this is a scripted message” which was piped into the script. Next, here is an example of assigning a value for the To parameter instead of the default.

“Hello, this is a scripted message” | ./send -to “Administrator@acme.com

Using the -To parameter name, we specify a value of the recipient, “administrator@acme.com“. Note however that because this is the first parameter declared in the parameter block of the script’s definition, we could actually have left out the “-to” parameter name and just passed the “administrator@acme.com” as a position parameter. Any of the parameters could be specified in the declared order: to, from, subject, or using their parameter names.

But how would we send something more interesting for the body of the message? Easily. We can use any command, cmdlet, PowerShell expression or pipeline to generate the body and just pipe it into the send script. For example:

type send.ps1 | ./send -subject “This is a little script to send messages.”

The above example uses the “type” alias for the Get-Content cmdlet to retrieve the contents of the file, actually this same script file being described herein, and pipes that to the send script, invoked with a specific subject.

With a bit of thought and some creativity, you can compose elaborate messages. For extended features such as attachments and multiple recipients, some minor changes could be made to this script to better utilize additional features of the CDO.Message class. An example worth investigating is using the ConvertTo-HTML cmdlet to convert tables to HTML content to be used as the body of a message. The input could be anything, from Get-Process, Get-Service, Get-ChildItem (dir, ls, gci), Get-WMIObject, and thousands of other possibilities.

What kinds of messages would you send with such a script? Hopefully this brief script helps you along the road to smoother systems administration.

-Brad

Related Courses

Automating Windows Server 2008 Administration with Windows Powershell (M6434)

Configuring, Managing, and Maintaining Server 2008 R2 (M6419)

Configuring, Managing, and Troubleshooting Microsoft Exchange Server 2010 (M10135)

Designing and Deploying Messaging Solutions with Microsoft Exchange Server 2010 (M10233)

Microsoft Exchange Server 2007 (M5047, M5049, M5051)

In this article

Join the Conversation