< Back
Add-DbaAgDatabase
Post
NAME Add-DbaAgDatabase
SYNOPSIS
Adds a database to an availability group on a SQL Server instance.
SYNTAX
Add-DbaAgDatabase [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [-AvailabilityGroup] <String> [[-Database] <System.String[]>] [[-Secondary]
<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SecondarySqlCredential] <Pscredential>]
[[-InputObject] <Microsoft.SqlServer.Management.Smo.Database[]>] [[-SeedingMode] <String>] [[-SharedPath]
<String>] [-UseLastBackup <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Adds a database to an availability group on a SQL Server instance.
Before joining the replica databases to the availability group, the databases will be initialized with automatic
seeding or full/log backup.
PARAMETERS
-AvailabilityGroup [<String>]
The availability group where the databases will be added.
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
-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
-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]
Enables piping from Get-DbaDatabase, Get-DbaDbSharePoint and more.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Secondary [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
Not required - the command will figure this out. But if you'd like to be explicit about replicas, this will
help.
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.
If not specified, the setting from the availability group replica will be used. Otherwise the setting will be
updated.
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
-SqlCredential [<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
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. Server version must be SQL Server version 2012 or higher.
This should be the primary replica.
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:\\>Add-DbaAgDatabase -SqlInstance sql2017a -AvailabilityGroup ag1 -Database db1, db2 -Confirm
Adds db1 and db2 to ag1 on sql2017a. Prompts for confirmation.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sql2017a | Out-GridView -Passthru | Add-DbaAgDatabase -AvailabilityGroup ag1
Adds selected databases from sql2017a to ag1
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Get-DbaDbSharePoint -SqlInstance sqlcluster | Add-DbaAgDatabase -AvailabilityGroup SharePoint
Adds SharePoint databases as found in SharePoint_Config on sqlcluster to ag1 on sqlcluster
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Get-DbaDbSharePoint -SqlInstance sqlcluster -ConfigDatabase SharePoint_Config_2019 | Add-DbaAgDatabase
-AvailabilityGroup SharePoint
Adds SharePoint databases as found in SharePoint_Config_2019 on sqlcluster to ag1 on sqlcluster
RELATED LINKS
https://dbatools.io/Add-DbaAgDatabase
SYNOPSIS
Adds a database to an availability group on a SQL Server instance.
SYNTAX
Add-DbaAgDatabase [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [-AvailabilityGroup] <String> [[-Database] <System.String[]>] [[-Secondary]
<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SecondarySqlCredential] <Pscredential>]
[[-InputObject] <Microsoft.SqlServer.Management.Smo.Database[]>] [[-SeedingMode] <String>] [[-SharedPath]
<String>] [-UseLastBackup <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Adds a database to an availability group on a SQL Server instance.
Before joining the replica databases to the availability group, the databases will be initialized with automatic
seeding or full/log backup.
PARAMETERS
-AvailabilityGroup [<String>]
The availability group where the databases will be added.
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
-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
-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]
Enables piping from Get-DbaDatabase, Get-DbaDbSharePoint and more.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Secondary [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
Not required - the command will figure this out. But if you'd like to be explicit about replicas, this will
help.
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.
If not specified, the setting from the availability group replica will be used. Otherwise the setting will be
updated.
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
-SqlCredential [<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
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. Server version must be SQL Server version 2012 or higher.
This should be the primary replica.
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:\\>Add-DbaAgDatabase -SqlInstance sql2017a -AvailabilityGroup ag1 -Database db1, db2 -Confirm
Adds db1 and db2 to ag1 on sql2017a. Prompts for confirmation.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Get-DbaDatabase -SqlInstance sql2017a | Out-GridView -Passthru | Add-DbaAgDatabase -AvailabilityGroup ag1
Adds selected databases from sql2017a to ag1
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Get-DbaDbSharePoint -SqlInstance sqlcluster | Add-DbaAgDatabase -AvailabilityGroup SharePoint
Adds SharePoint databases as found in SharePoint_Config on sqlcluster to ag1 on sqlcluster
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Get-DbaDbSharePoint -SqlInstance sqlcluster -ConfigDatabase SharePoint_Config_2019 | Add-DbaAgDatabase
-AvailabilityGroup SharePoint
Adds SharePoint databases as found in SharePoint_Config_2019 on sqlcluster to ag1 on sqlcluster
RELATED LINKS
https://dbatools.io/Add-DbaAgDatabase