Add-sqmDatabaseToAG
Always On
sqmSQLTool v1.8.2+ · Add
✓ -WhatIf supported
Add-DbaAgDatabase -SeedingMode Automatic. When -All is used, databases are added sequentially to avoid simultaneous seeding load.
Prerequisite: Automatic Seeding must already be configured on all replicas. Use
Invoke-sqmSqlAlwaysOnAutoseeding if not yet set up.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Primary SQL Server instance. |
| -SqlCredential | PSCredential | Optional | — | SQL or Windows credential. |
| -AvailabilityGroup | string | Required | — | Name of the target availability group. |
| -Database | string[] | Optional | — | One or more database names. ParameterSet: Specific. Ignored when -All is set. |
| -All | switch | Switch | $false | Add all accessible user databases not yet in any AG. ParameterSet: All. |
| -EnableException | switch | Switch | $false | Throw on error instead of logging and continuing. |
| -WhatIf / -Confirm | switch | Optional | — | Standard PowerShell ShouldProcess support. All three state-changing operations (RecoveryModel, Drop, Add) are individually gated. |
Execution Flow
Examples
Add a single database to an AG
Add-sqmDatabaseToAG -AvailabilityGroup "AG1" -Database "SalesDB"
Add all user databases not yet in any AG
Add-sqmDatabaseToAG -SqlInstance "SQL01" -AvailabilityGroup "AG1" -All
Preview without making changes (-WhatIf)
Add-sqmDatabaseToAG -SqlInstance "SQL01" -AvailabilityGroup "AG1" -All -WhatIf
Add multiple named databases with SQL credentials
$cred = Get-Credential
Add-sqmDatabaseToAG -SqlInstance "SQL01" -SqlCredential $cred `
-AvailabilityGroup "AG_PROD" -Database "DB1","DB2","DB3"