Creating a SQL Server Availability Group via PowerShell

This is tangibly related to SharePoint and this took me awhile to figure out the correct syntax.  I’ve been building a SQL AG on Server 2012 with the Core installation option.  To accomplish this, you will need a workstation with SQL Management Studio installed (which also installed the SQL PowerShell module).  Some background on this setup:

2 servers with Windows Server 2012 and SQL Server 2012 SP1 CU5

3 network adapters per server

Features installed (note that “D:” has the Windows Server ISO loaded) on the servers:

IP all of the adapters appropriately.  Next, rename the secondary and tertiary network adapters for clarity:

Note that you may want to disable or modify the Firewall Policy for the “Domain” and “Private” profiles.  You can do this via the Set-NetFirewallProfile cmdlet.

On the client workstation with the RSAT tools installed (Failover Cluster), run the following to import the Failover Cluster module, create the cluster with a static IP address, then rename the network adapters to fit the adapter name on the server.  Finally, set the Ethernet adapter (this is the adapter that is used for standard client to server communication) Host Record TTL to 300 seconds.

Next, on each server, install SQL via batch script.  Real quick hint here, make sure to exit the PowerShell prompt prior to running this.  The SQL installation media needs .NET 3.5, but because we’re running Server 2012 and PowerShell 3.0, we’re using .NET 4.  This will cause the installation to fail.  The following will install SQL Server 2012 with the Database Engine, SQL Agent, Replication, Integration Services, and Client Connectivity with the specified username and password to the E: drive while the CD/ISO is present in the D: drive.  Do not forget to edit the /PID value.  It will also enable TCP/IP connectivity to the instance.

Once complete, download SQL Server 2012 SP1 and I’ve chosen to also install CU5.  Once downloaded, extract the executable using the following command, making sure to extract each package to a unique path:

Next, for each package, run:

This will install the package applying the patch to all instances on the server.  You may or may not need to reboot in between patches.

Next, move to the workstation.  I have a workstation running Windows 8 x64 with SQL Management Studio 2012 SP1.  Open Management Studio and go to View -> Registered Servers.  Add the two instances under Local Server Groups.

Next, run PowerShell as Administrator on the Workstation.  Run “sqlps” or  Import-Module sqlps to import the SQL PowerShell module.  Validate the previous server registration by executing  cd SQLSERVER:\SQLRegistration\"Database Engine Server Group" , and running ls .  Both servers should appear here.  The next step is to connect to each server, which can be done by executing  cd SQLSERVER:\SQL\SERVERNAME .

For each SQL Server, the next step is to enable AlwaysOn and create the HADR Endpoint.  Both cmdlets have a few options, so review them prior to execution.  Note that when enabling AlwaysOn, the Database Engine service must be restarted, which the -Force switch does (or should do, it didn’t work in my case).

Next, create the Availability Replicas (in memory), create the Availability Group with the Primary server specified, and finally Join the secondary server to the Availability Group.  Again, these cmdlets have a lot of options, so it is best to review them so the setup fits your environment.

After this, via Management Studio, you should now be able to review the Availability Group status.  In my case, I had a critical error which was due to my HADR endpoint being in a stopped state, preventing the secondary replica from connecting to the primary.  To resolve this, I ran the following T-SQL:

Once this completed, the secondary replica joined automatically.  Note it is normal to have warnings as there are no synchronized databases at this point.

The final step to create the Availability Group is to create the Listener, which can be done with the following cmdlet:

And now you have two complete SQL Servers, ready to have SharePoint databases added to the Aavailabity Group!  Make sure to test failover to validate functionality.  Do not forget the following resources with regards to supportability of SharePoint databases on an Availability Group.

Supported high availability and disaster recovery options for SharePoint databases (SharePoint 2013)

Configure SQL Server 2012 AlwaysOn Availability Groups for SharePoint 2013

Leave a Reply