Prof. Powershell

It's In the Mail Part 1: Sending PowerShell Data via E-Mail

It is very easy to get a lot of information out of PowerShell. But sometimes it isn't as easy to get it where it needs to be. One solution is e-mail. Everyone has it so why not send data pulled from PowerShell via an e-mail message? It is very easy with the Send-MailMessage cmdlet. In fact, you don't even need to include a message body. This is the bare minimum that you need:

PS C:\> Send-MailMessage -To [email protected] -from [email protected] -subject "Is this thing on?" –Smtp Server mail.jdhitsolutions.com

You should be able to specify the same SMTP server you use with your primary e-mail client. Since you are most likely sending from and to the same domain you shouldn't run into any issues. But it is possible the mail server may be locked down and disallow message forwarding. You might need to work with mail server administrators to sort out some details. The cmdlet supports options for credentials and to use SSL if that is necessary as well. I'm going to assume the basics will work just fine.

Typing the SMTP server all the time can get tedious, but fortunately the cmdlet will look for a default variable called PSE-mailServer. If found, it will sue that value for the cmdlet. In your profile simply add a line like this:

$PSE-mailServer = 'mail.mycompany.com'

In PowerShell 3.0, if you can also set defaults for values like TO and FROM in your profile:

$PSDefaultParameterValues.Add("Send-MailMessage:From","[email protected]"; "Send-MailMessage:To",[email protected])

In PowerShell 2.0 the cmdlet defaults to port 25. In PowerShell 3.0 you can specify a port which comes in handy when using services like Gmail.

Now that we have the mechanics let's send some stuff. One gotcha that I want to cover is that whatever you send as the message body must be a string. If you try a command like this, it will fail. Note that I'm running Powershell 3.0 and using default values for TO and FROM and have $PSE-mailServer configured.

PS C:\> send-mailmessage -subject "Service Report" -body (get-service)

You'll get an error about failing to convert System.Object[] to System.String. The solution is to pipe the command to Out-String.

PS C:\> send-mailmessage -subject "Service Report" -body (get-service | out-string)

Or if you want to build a longer message, I suggest using a here-string.

$body=@"
Here is the service report for CHI-DC02
$(get-service -ComputerName CHI-DC02 | out-string)
Please report any problems immediately.
Jeff
IT Help Desk Monkey
"@

Send-MailMessage -Subject "Service Report" -Body $body -Priority High

In these situations, the message body is plain text. But you might want to send the report as an e-mail attachment instead. To do that, naturally you first need a file.

$computers = "chi-dc01","chi-dc02","chi-dc04"
$data = Get-CimInstance win32_operatingsystem -filter "Status = 'OK'" -comp $computers
$data | Select PSComputername,@{Name="OS";Expression={$_.Caption}},LastBootuptime,@{Name="Uptime";Expression={(Get-Date)-$_.lastbootuptime}} | format-table -AutoSize | Out-File -Encoding ascii -Width 120 -FilePath c:\work\osreport.txt

This will create a text file with some uptime information. Now I can send it like this.

Send-MailMessage -Subject "Daily Uptime Report" -Body "Please see the attached report" -Attachments C:\work\osreport.txt

If you want to send more than one attachment simply separate each file path by a comma.

Using Send-Mailmessage is a simple and convenient way to get information to people who need it. Be aware that none of this involves your e-mail client in any way. None of the sent messages will show up as sent items in your mail client. All you should need is the recipient's e-mail address. Although you can customize the TO and FROM like this:

PS C:\> send-mailmessage -to "Prof. PowerShell <[email protected]>" -Subject "Training" -Body "Please contact me regarding some PowerShell training."

I also didn't mention using CC and BCC since their use is the same as TO and FROM. Next time we'll look at some fancier messaging using HTML.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular