< Back

New-DbaAvailabilityGroup

Mon Jan 13, 2020 12:40 pm

NAME New-DbaAvailabilityGroup



SYNOPSIS

Automates the creation of availability groups.





SYNTAX

New-DbaAvailabilityGroup [[-Primary] <DbaInstanceParameter>] [[-PrimarySqlCredential] <Pscredential>]

[[-Secondary] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SecondarySqlCredential]

<Pscredential>] [-Name] <String> [-DtcSupport <Switch>] [[-ClusterType] <String>] [[-AutomatedBackupPreference]

<String>] [[-FailureConditionLevel] <String>] [[-HealthCheckTimeout] <Int>] [-Basic <Switch>]

[-DatabaseHealthTrigger <Switch>] [-Passthru <Switch>] [[-Database] <System.String[]>] [[-SharedPath] <String>]

[-UseLastBackup <Switch>] [-Force <Switch>] [[-AvailabilityMode] <String>] [[-FailoverMode] <String>]

[[-BackupPriority] <Int>] [[-ConnectionModeInPrimaryRole] <String>] [[-ConnectionModeInSecondaryRole] <String>]

[[-SeedingMode] <String>] [[-Endpoint] <String>] [[-ReadonlyRoutingConnectionUrl] <String>] [[-Certificate]

<String>] [[-IPAddress] <System.Net.IPAddress[]>] [[-SubnetMask] <Ipaddress>] [[-Port] <Int>] [-Dhcp <Switch>]

[-EnableException <Switch>] [<CommonParameters>]





DESCRIPTION

Automates the creation of availability groups.



* Checks prerequisites



* Creates Availability Group and adds primary replica



* Grants cluster permissions if necessary



* Adds secondary replica if supplied



* Adds databases if supplied



* Performs backup/restore if seeding mode is manual



* Performs backup to NUL if seeding mode is automatic



* Adds listener to primary if supplied



* Joins secondaries to availability group



* Grants endpoint connect permissions to service accounts



* Grants CreateAnyDatabase permissions if seeding mode is automatic



* Returns Availability Group object from primary



NOTE: If a backup / restore is performed, the backups will be left intact on the network share.



Thanks for this, Thomas Stringer! https://blogs.technet.microsoft.com/hey ... n-alwayson

-availability-group-with-powershell/





PARAMETERS

-AutomatedBackupPreference [<String>]

Specifies how replicas in the primary role are treated in the evaluation to pick the desired replica to

perform a backup.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-AvailabilityMode [<String>]

Sets the availability mode of the availability group replica. Options are: AsynchronousCommit and

SynchronousCommit. SynchronousCommit is default.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-BackupPriority [<Int>]

Sets the backup priority availability group replica. Default is 50.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Basic [<Switch>]

Indicates whether the availability group is basic. Basic availability groups like pumpkin spice and uggs.



https://docs.microsoft.com/en-us/sql/da ... roups-alwa

ys-on-availability-groups



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Certificate [<String>]

Specifies that the endpoint is to authenticate the connection using the certificate specified by

certificate_name to establish identity for authorization.



The far endpoint must have a certificate with the public key matching the private key of the specified

certificate.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ClusterType [<String>]

Cluster type of the Availability Group. Only supported in SQL Server 2017 and above.

Options include: External, Wsfc or None. None by default.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ConnectionModeInPrimaryRole [<String>]

Specifies the connection intent modes of an Availability Replica in primary role. AllowAllConnections by

default.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ConnectionModeInSecondaryRole [<String>]

Specifies the connection modes of an Availability Replica in secondary role. AllowAllConnections by default.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<System.String[]>]

The database or databases to add.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-DatabaseHealthTrigger [<Switch>]

Indicates whether the availability group triggers the database health.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Dhcp [<Switch>]

Indicates whether the object is DHCP.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-DtcSupport [<Switch>]

Indicates whether the DtcSupport is enabled



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.

This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables

advanced scripting.

Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own

try/catch.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Endpoint [<String>]

By default, this command will attempt to find a DatabaseMirror endpoint. If one does not exist, it will create

it.



If an endpoint must be created, the name "hadr_endpoint" will be used. If an alternative is preferred, use

Endpoint.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-FailoverMode [<String>]

Sets the failover mode of the availability group replica. Options are Automatic, Manual and External.

Automatic is default.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-FailureConditionLevel [<String>]

Specifies the different conditions that can trigger an automatic failover in Availability Group.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Force [<Switch>]

Drop and recreate the database on remote servers using fresh backup.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-HealthCheckTimeout [<Int>]

This setting used to specify the length of time, in milliseconds, that the SQL Server resource DLL should wait

for information returned by the sp_server_diagnostics stored procedure before reporting the Always On Failover

Cluster Instance (FCI) as unresponsive.



Changes that are made to the timeout settings are effective immediately and do not require a restart of the

SQL Server resource.



Defaults to 30000 (30 seconds).



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-IPAddress [<System.Net.IPAddress[]>]

Sets the IP address of the availability group listener.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Name [<String>]

The name of the Availability Group.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Passthru [<Switch>]

Don't create the availability group, just pass thru an object that can be further customized before creation.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Port [<Int>]

Sets the number of the port used to communicate with the availability group.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Primary [<DbaInstanceParameter>]

The primary SQL Server instance. Server version must be SQL Server version 2012 or higher.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-PrimarySqlCredential [<Pscredential>]

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).



Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory -

Integrated are all supported.



For MFA support, please use Connect-DbaInstance.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ReadonlyRoutingConnectionUrl [<String>]

Sets the read only routing connection url for the availability replica.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Secondary [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances. Server version must be SQL Server version 2012 or higher.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SecondarySqlCredential [<Pscredential>]

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).



Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory -

Integrated are all supported.



For MFA support, please use Connect-DbaInstance.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SeedingMode [<String>]

Specifies how the secondary replica will be initially seeded.



Automatic enables direct seeding. This method will seed the secondary replica over the network. This method

does not require you to backup and restore a copy of the primary database on the replica.



Manual requires you to create a backup of the database on the primary replica and manually restore that backup

on the secondary replica.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SharedPath [<String>]

The network share where the backups will be backed up and restored from.



Each SQL Server service account must have access to this share.



NOTE: If a backup / restore is performed, the backups will be left in tact on the network share.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SubnetMask [<Ipaddress>]

Sets the subnet IP mask of the availability group listener.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-UseLastBackup [<Switch>]

Use the last full backup of database.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: AvailabilityGroup, HA, AG

Author: Chrissy LeMaire (@cl), netnerds.net



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql2016a -Name SharePoint



Creates a new availability group on sql2016a named SharePoint

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql2016a -Name SharePoint -Secondary sql2016b



Creates a new availability group on sql2016a named SharePoint with a secondary replica, sql2016b

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql2016std -Name BAG1 -Basic -Confirm:$false



Creates a basic availability group named BAG1 on sql2016std and does not confirm when setting up

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql2016b -Name AG1 -ClusterType Wsfc -Dhcp -Database db1 -UseLastBackup



Creates an availability group on sql2016b with the name ag1. Uses the last backups available to add the database

db1 to the AG.

-------------------------- EXAMPLE 5 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql2017 -Name SharePoint -ClusterType None -FailoverMode Manual



Creates a new availability group on sql2017 named SharePoint with a cluster type of none and a failover mode of

manual

-------------------------- EXAMPLE 6 --------------------------



PS C:\\>New-DbaAvailabilityGroup -Primary sql1 -Secondary sql2 -Name ag1 -Database pubs -ClusterType None

-SeedingMode Automatic -FailoverMode Manual



Creates a new availability group with a primary replica on sql1 and a secondary on sql2. Automatically adds the

database pubs.

-------------------------- EXAMPLE 7 --------------------------



PS C:\\>$cred = Get-Credential sqladmin



PS C:\\> $params = @{

>> Primary = "sql1"

>> PrimarySqlCredential = $cred

>> Secondary = "sql2"

>> SecondarySqlCredential = $cred

>> Name = "test-ag"

>> Database = "pubs"

>> ClusterType = "None"

>> SeedingMode = "Automatic"

>> FailoverMode = "Manual"

>> Confirm = $false

>> }

PS C:\\> New-DbaAvailabilityGroup @params



This exact command was used to create an availability group on docker!



RELATED LINKS

https://dbatools.io/New-DbaAvailabilityGroup