dbatools

Sync-DbaAvailabilityGroup

View Source
Chrissy LeMaire (@cl), netnerds.net
Windows, Linux, macOS

Synopsis

Synchronizes server-level objects from primary to secondary replicas in availability groups

Description

Copies server-level objects from the primary replica to all secondary replicas in an availability group. Availability groups only synchronize databases, not the server-level dependencies that applications need to function properly after failover.

This command ensures that logins, SQL Agent jobs, linked servers, and other critical server objects exist on all replicas so your applications work seamlessly regardless of which replica becomes primary. By default, it synchronizes these object types:

SpConfigure
CustomErrors
Credentials
DatabaseMail
LinkedServers
Logins
LoginPermissions
SystemTriggers
DatabaseOwner
AgentCategory
AgentOperator
AgentAlert
AgentProxy
AgentSchedule
AgentJob

Any of these object types can be excluded using the -Exclude parameter. For granular control over specific objects (like excluding individual jobs or logins), use the -ExcludeJob, -ExcludeLogin parameters or the underlying Copy-Dba* commands directly.

The command copies ALL objects of each enabled type - it doesn’t filter based on which objects are actually used by the availability group databases. Use the exclusion parameters to limit scope when needed.

Syntax

Sync-DbaAvailabilityGroup
    [[-Primary] <DbaInstanceParameter>]
    [[-PrimarySqlCredential] <PSCredential>]
    [[-Secondary] <DbaInstanceParameter[]>]
    [[-SecondarySqlCredential] <PSCredential>]
    [[-AvailabilityGroup] <String>]
    [[-Exclude] <String[]>]
    [[-Login] <String[]>]
    [[-ExcludeLogin] <String[]>]
    [[-Job] <String[]>]
    [[-ExcludeJob] <String[]>]
    [-DisableJobOnDestination]
    [[-InputObject] <AvailabilityGroup[]>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Sync-DbaAvailabilityGroup -Primary sql2016a -AvailabilityGroup db3

Syncs the following on all replicas found in the db3 AG:
SpConfigure, CustomErrors, Credentials, DatabaseMail, LinkedServers
Logins, LoginPermissions, SystemTriggers, DatabaseOwner, AgentCategory,
AgentOperator, AgentAlert, AgentProxy, AgentSchedule, AgentJob

Example: 2
PS C:\> Get-DbaAvailabilityGroup -SqlInstance sql2016a | Sync-DbaAvailabilityGroup -ExcludeType LoginPermissions, LinkedServers -ExcludeLogin login1, login2 -Job job1, job2

Syncs the following on all replicas found in all AGs on the specified instance:
SpConfigure, CustomErrors, Credentials, DatabaseMail, Logins,
SystemTriggers, DatabaseOwner, AgentCategory, AgentOperator
AgentAlert, AgentProxy, AgentSchedule, AgentJob.
Copies all logins except for login1 and login2 and only syncs job1 and job2

Example: 3
PS C:\> Get-DbaAvailabilityGroup -SqlInstance sql2016a | Sync-DbaAvailabilityGroup -WhatIf

Shows what would happen if the command were to run but doesn’t actually perform the action.

Optional Parameters

-Primary

The primary replica SQL Server instance for the availability group. This is the source server from which all server-level objects will be copied.
Required when not using InputObject parameter. Server version must be SQL Server 2012 or higher.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-PrimarySqlCredential

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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Secondary

The secondary replica SQL Server instances where server-level objects will be copied to. Can specify multiple instances.
If not specified, the function will automatically discover all secondary replicas in the availability group. Server version must be SQL Server 2012 or higher.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-SecondarySqlCredential

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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-AvailabilityGroup

The name of the specific availability group to synchronize server objects for.
When specified, the function will identify all replicas in this AG and sync objects from primary to all secondaries.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Exclude

Excludes specific object types from being synchronized to avoid conflicts or reduce sync time.
Useful when you need to manually manage certain objects or when some object types cause issues in your environment. Valid values:
SpConfigure, CustomErrors, Credentials, DatabaseMail, LinkedServers, Logins, LoginPermissions,
SystemTriggers, DatabaseOwner, AgentCategory, AgentOperator, AgentAlert, AgentProxy, AgentSchedule, AgentJob

PropertyValue
AliasExcludeType
RequiredFalse
Pipelinefalse
Default Value
Accepted ValuesAgentCategory,AgentOperator,AgentAlert,AgentProxy,AgentSchedule,AgentJob,Credentials,CustomErrors,DatabaseMail,DatabaseOwner,LinkedServers,Logins,LoginPermissions,SpConfigure,SystemTriggers
-Login

Specifies which login accounts to synchronize to secondary replicas. Accepts an array of login names.
Use this when you only need to sync specific service accounts or application logins rather than all logins on the server.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-ExcludeLogin

Specifies login accounts to skip during synchronization. Accepts an array of login names.
Commonly used to exclude system accounts, sa, or logins that should remain unique per replica for monitoring or maintenance purposes.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Job

Specifies which SQL Agent jobs to synchronize to secondary replicas. Accepts an array of job names.
Use this when you only need to sync critical jobs like backup jobs or maintenance tasks rather than all jobs on the server.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-ExcludeJob

Specifies SQL Agent jobs to skip during synchronization. Accepts an array of job names.
Commonly used to exclude replica-specific jobs like log shipping, local backups, or jobs that should only run on the primary replica.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-DisableJobOnDestination

Disables all synchronized jobs on secondary replicas after copying them from the primary.
Use this when jobs should only run on the primary replica or when you need to manually control which jobs run on each replica after failover.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-InputObject

Accepts availability group objects from Get-DbaAvailabilityGroup for pipeline processing.
Use this to sync multiple availability groups at once or to process specific AGs returned by filtering commands.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-Force

Drops and recreates existing objects on secondary replicas instead of skipping them.
Use this when you need to update objects that already exist on secondaries or when objects have configuration differences that need to be synchronized.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-EnableException

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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value