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: http://gallery.technet.microsoft.com/scriptcenter/Configuring-a-Server-for-0b95eb8e.

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:

http://blogs.msdn.com/b/sqlalwayson/archive/2012/02/03/configuring-alwayson-with-sql-server-powershell.aspx

 

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