Dev Notes

Notes on Development with Microsoft Technologies

Send Email using PowerShell

2 Comments

While this isn’t directly related to SharePoint, there may be some times that you need to run a long running operation with PowerShell and would like to receive an email notification when the operation is completed. Here is a quick function you can place into most any script and then call to send notifications.

Since most SharePoint implementations are already configured to allow email notifications to be sent (alerts) you may be able to allow your scripts to also send you alerts. Here’s the script:

function send-notification($subject, $detail) {
    $message = New-Object System.Net.Mail.MailMessage
    $message.Subject = $subject
    $message.Body = $detail
    $message.To.Add("recipient@domain.com")
    $message.From = "sender@domain.com"
    
    $client = New-Object System.Net.Mail.SMTPClient -ArgumentList "smtp.domain.com"
    $client.Send($message)
}

To make use of the function, just simply call it with the necessary parameters:

send-notification -subject "$dbname Attached" -detail "The content database $dbname has completed the database attach upgrade. Please review the logs in Central Administration as soon as possible."

Enjoy!

Edit 12/8/2011: If you would like to send to multiple users, you can do the following:

$message.To.Add("nextemail@domain.com")
$message.To.Add("anotherperson@domain.com")
#You can also CC and BCC
$message.Cc.Add("firstcc@domain.com")
$message.Bcc.Add("firstbcc@domain.com")
Advertisements

Author: Chris Quick

I have been a developer of web based solutions since early 2001 delivering solutions to a wide array of organizations using ASP, ASP.NET and SharePoint. I was introduced to SharePoint in 2003 when the consulting firm I worked for at the time introduced it into the workplace. I began working with MOSS 2007 as soon as Microsoft released the RTM version in November 2006. The platform was implemented at the organization I worked for in 2007 and went live in March of that year. I was tasked with the administration and ongoing development of the platform. I currently work as a SharePoint Architect with Artis Consulting, developing solutions for a wide variety of business problems. The goal of this blog is to share my discoveries developing solutions with SharePoint. I welcome your comments and feedback to any post -- and I welcome suggestions for future topics.

2 thoughts on “Send Email using PowerShell

  1. is it jquery?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s