This script demonstrates how to create an availability group using cmdlets introduced in SQL Server 2012.  This script assumes you have a group of machines joined to a Windows Server Failover Cluster, and SQL Server 2012 instances running on these machines.  These server instances should have AlwaysOn enabled and should have database mirroring endpoints created and configured --  refer to this script for guidance on how to automate this process:

Besides creating the availability group proper, this script demonstrates some of the related tasks necessary to get an availability group up and running, such as: restoring databases and their logs on all secondary replicas, and joining secondary replicas and databases to the availability group. 

This script must be executed inside the SQLPS.exe shell.  It should be executed by a domain administrator who also has administrative rights on the targeted SQL Server instances.

Here is a blog post discussing this script in more detail:


# Example usage 
.\createag -ServerList "Server1\Instance1""Server2\Instance2" -BackupShare "c:\backups"