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

Setting up Database Mail


This recipe demonstrates how to set up Database Mail programmatically using PowerShell.

Getting ready

The assumption in this recipe is that Database Mail is not yet configured on your instance.

These are the settings we will use for this recipe:

Setting

Value

Mail Server

mail.queryworks.local

Mail Server Port

25

E-mail address for Database Mail profile

[email protected]

SMTP authentication

Basic authentication

Credentials for e-mail address

Username is [email protected]

Password is <some password>

How to do it...

To set up Database Mail, follow these steps:

  1. Open PowerShell ISE as administrator.

  2. Import the SQLPS module and create a new SMO Server object:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your actual server name
    $instanceName = "localhost"
    
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName

    Add the following script block to create a Database...