August 21, 2009

What's your favorite PICKUP line?
[C'mon, everyone has one, so fess up!]

Ours (at least in the office) is:
copy serverstatus.eml \\myTransportServer\Pickup

Do you have a need for an easy method of sending a message from within your Powershell scripts? Do your automated procedures report their results to you via email? Do you want them to? Details? Summaries? Have you used the pickup folder to send messages from your monitoring or mailbox processing scripts? If not, you should be. Read on to see how easy the process can be, especially for you Exchange Admins.

Here at ITEC, we host mailboxes for a few campuses and SUNY entities. Automating procedures helps us to provide consistent, and faster service to our customers. Scripting as many procedures as possible helps in standardization and minimizes the chance for error. However, automated processes can sometimes go on without monitoring and without an administrative eye watching over them. Notification from these automated procedures is sometimes critical to system health as well as customer satisfaction. An easy way to use your Exchange environment to deliver status messages and reports is to use the PICKUP folder on your Exchange Transport server.

First, we should create a share that points to the pickup folder on one of your transport servers to use as your drop folder for system status messages.


Format your message following RFC-822 standards as shown below, naming it with an .eml extension, then copy it to the pickup share to be delivered by the Exchange transport server. We use a Powershell function (discussed later in this article) that we've written to make our mailer procedure transportable, but you can easily put the code inline in your reporting script.

From:
To:
S
ubject:

Message body here. (Content from your report file)


Make sure there is a blank line between the Subject: line and the start of your message body... that's all there is to it. Exchange monitors the pickup folder for new messages and processes them as long as they are formatted correctly. We use a script to build the output file with very simple Powershell write-output cmdlets.


Building the email message takes no more than the few Powershell lines shown below. After the output .eml message file is created, it's simply a matter of copying the file to the pickup share for processing.


$Msg = "MyMessage.eml"
$MsgBody = Get-Content C:\temp\MyReport.txt
$MsgDisclaimer = "Some company mumbo jumbo..."
write-output "From: $fromaddr" out-file $Msg -encoding ASCII
write-output "To: $toaddr" out-file $Msg -encoding ASCII -append
write-output "Subject: $subj" out-file $Msg -encoding ASCII -append
write-output "`r`n" out-file $Msg -encoding ASCII -append
write-output " -Automated Msg -" out-file $Msg -encoding ASCII -append
write-output $MsgBody out-file $Msg -encoding ASCII -append
write-output $MsgDisclaimer out-file $Msg -encoding ASCII -append
Move-Item $Msg -destination \\MyTransportServer\PickupShare\


Using a function to build and send the message makes calling the mailer easier and allows us to use it in many different procedures. Our version of the function (to be discussed in a future post see Note below) includes detailed help as we anticipate the function being used by several staff from within many different scripts.




Note: The post to discuss ITEC's custom function for sending using the PICKUP folder will be replaced by a post about the Powershell v2 cmdlet Send-MailMessage, which we will discuss instead.


.end

more...