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

Monitoring the health of an Availability Group


In this recipe, we will see a few snippets that will help us monitor the health of an AlwaysOn Availability Group.

Getting ready

To follow this recipe, your AlwaysOn availability group should already be set up and the primary/secondary replicas should be functional. If not, you can use the previous recipes in this chapter to set up your AlwaysOn Availability Group.

In PowerShell, there are three cmdlets that we will use to test AlwaysOn health. To use these cmdlets, you need the following permissions:

  • CONNECT

  • VIEW SERVER STATE

  • VIEW ANY DEFINITION

How to do it...

We will see how to monitor an Availability Group's health using PowerShell by following these steps:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    Import-Module SQLPS -DisableNameChecking
    
    $instanceName = "SQL01"
    $AGName = "SQLAG"
    
    #SMO server object
    $server = New-Object Microsoft.SqlServer.Management.Smo.Server  $instanceName
    
    #test availability group health...