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

Adding an availability database to an Availability Group


In this recipe, we will add an availability database to an Availability Group.

Getting ready

Before we proceed with this recipe, ensure that you have already created the availability group in the primary instance.

The prerequisites for adding an availability database, as documented on MSDN, are as follows:

  • You must be connected to the server instance that hosts the primary replica

  • The database must reside on the server instance that hosts the primary replica and comply with the prerequisites and restrictions for availability databases

How to do it...

Perform the following steps to add an availability database to an existing availability group:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    Import-Module SQLPS -DisableNameChecking
    
    $AGName = "SQLAG"
    $dbName = "QueryWorksDB"
    $secondaryList = @("SQL02", "SQL03")
    $cred = Get-Credential
    
    foreach ($secondary in $secondaryList)
    {
    
        $server = New-Object Microsoft.SqlServer...