Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Sending an e-mail


In this recipe, we send an e-mail with an attachment.

Getting ready

Before proceeding, identify the following in your environment:

  • An SMTP server

  • A recipient's e-mail address

  • A sender's e-mail address

  • An attachment

How to do it...

These are the steps required to send an e-mail:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    $file = "C:\Temp\processes.csv"
    $timestamp = Get-Date -format "yyyy-MMM-dd-hhmmtt"
    
    #note we are using backticks to put each parameter
    #in its own line to make code more readable
    Send-MailMessage `
    -SmtpServer "queryworks.local" `
    -To "[email protected]" `
    -From "[email protected]" `
    -Subject "Process Email - $file - $timestamp" `
    -Body "Here ya go" `
    -Attachments $file

How it works...

One way to send an e-mail using PowerShell is using the Send-MailMessage cmdlet. Some of the switches it accepts are as follows:

  • -SmtpServer

  • -To

  • -Cc

  • -Bcc

  • -Credential

  • -From

  • -Subject

  • -Body

  • -Attachments

  • -UseSsl

There's more...