PowerShell send email

PowerShell send emailSometimes when scheduling scripts on a server it could be useful to have PowerShell send email automatically with command results or even content of files, in previous versions of PowerShell it was necessary to leverage .Net classes to achieve this, luckily enough all of this has been integrated into native PowerShell functionalities let’s see how it works!

PowerShell Send email – The Send-MailMessage cmdlet

As I mentioned PowerShell has a cmdlet dedicated to sending emails the Send-MailMessage cmdlet below you can see an extract of the cmdlet help:

NAME
    Send-MailMessage

SYNOPSIS
    Sends an e-mail message.


SYNTAX
    Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-
    Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNot
    ificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | H
    igh}] [-UseSsl] [<CommonParameters>]


DESCRIPTION
    The Send-MailMessage cmdlet sends an e-mail message from within Windows PowerShell.

Let’s see how we can use the cmdlet to send messages for our scripts, when running the command interactively you can use the following syntax to send a message

Send-MailMessage -To helocheck@helocheck.com -Subject "PowerShell send email" -From helocheck@helocheck.com -SmtpServer mysmtp.helocheck.com -body "PowerShell can send email messages!"

The above is just an example but will send a mail message to the desired recipient, the cmdlet supports authentication, ssl and even attachemnts which is handy if you need to receive copy of a log file for troubleshooting or analysis tools.

PowerShell Send email – How to send email via scripts

The above will work nicely if you’re using PowerShell to send email messages interactively what if you need to integrate the above in a script that will run unattended? As a matter of fact I find myself doing this more often than not and have developed a small function that I integrate in most of my scripts adjusting the various parameters depending on customer’s environment

function sendMail()
{
	$smartHost = "smtp.yourdomain.com"
	$sendTo = "helocheck@helocheck.com" #Multiple values separated by comma
	$sendFrom = "PowerShell-Report@helocheck.com"
        $mailAttachment = C:some_file.txt
	$mailSubject = "PowerShell Report"
	$mailBody = "PowerShell send email via script"
	Send-MailMessage -Subject $mailSubject -From $sendFrom -To $sendTo -SmtpServer $smartHost -body $mailBody -Attachments $mailAttachment
} #End function sendMail

While the above is a really simple function it helps you save time when implementing scripts, all you have to do is to change variables values adapting to your environment.

In the next post I will go through the process of using the output of a PowerShell command as the body of the message.

Advertisements

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