Add-sqmDatabaseToDistributedAg
Always On
sqmSQLTool v1.8.2+ · Add
✓ -WhatIf supported
NORECOVERY, join the restored database to the secondary AG, and finally monitor synchronization state.
Each step is recorded in a
Steps list on the returned [PSCustomObject]. On success the object carries Status = 'SUCCESS'; any unhandled error returns Status = 'FAILED' with the error message.
Prerequisites: A secondary AG and the Distributed AG relationship must already be established. The
-BackupPath must be reachable from both instances (UNC share or local path with appropriate permissions).
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Primary SQL Server instance (source). Accepts pipeline input. |
| -AvailabilityGroupName | string | Required | — | Name of the Distributed AG. |
| -DatabaseName | string | Required | — | Database to add to the Distributed AG. |
| -SecondaryInstance | string | Required | — | SQL Server instance on the secondary cluster where the database is restored. |
| -BackupPath | string | Optional | C:\Backups | Directory for full (.bak) and log (.trn) backup files. Created automatically if not present. |
| -SqlCredential | PSCredential | Optional | — | Used for all connections (primary and secondary). |
| -EnableException | switch | Switch | $false | Re-throw errors instead of returning a FAILED result object. |
| -WhatIf / -Confirm | switch | Optional | — | Standard ShouldProcess support (ConfirmImpact: High). |
Execution Flow
Examples
Add a database to a Distributed AG via a shared backup path
Add-sqmDatabaseToDistributedAg `
-SqlInstance "SQL01" `
-AvailabilityGroupName "MyDAG" `
-DatabaseName "MyDb" `
-SecondaryInstance "DR-SQL01" `
-BackupPath "\\FileServer\SQLBackups"
Capture step results and check overall status
$result = Add-sqmDatabaseToDistributedAg -SqlInstance "SQL01" -AvailabilityGroupName "MyDAG" `
-DatabaseName "MyDb" -SecondaryInstance "DR-SQL01"
if ($result.Status -ne 'SUCCESS') {
Write-Warning "Failed: $($result.Error)"
$result.Steps | Format-Table
}