Performs a controlled failover of a Distributed Availability Group. Validates readiness (score ≥ 75/100 via Test-sqmDistributedAgReadiness), discovers the secondary AG from sys.availability_replicas, issues ALTER AVAILABILITY GROUP … FAILOVER, verifies the new primary role, and exports a detailed TXT report. Use -Rollback to fail back after a failed migration without the readiness check.
Execution Flow
Synopsis
For a normal failover, Test-sqmDistributedAgReadiness is called first; a ReadinessScore below 75 aborts the operation. The secondary AG and its server are discovered via a T-SQL query against sys.availability_groups (SQL 2016 compatible — no dm_hadr_distributed_ag_replica_member_status). Without -Force, a ShouldProcess prompt is shown. The failover uses ALTER AVAILABILITY GROUP … FAILOVER (no FORCE_FAILOVER_ALLOW_DATA_LOSS). After a 3-second pause, replica roles are verified and a TXT report is written to -OutputPath.
-Rollback skips the readiness check and is intended for fail-back after a failed migration. Use with caution — it performs the same FAILOVER SQL statement.
Critical operation. Always test in a DR environment first. The readiness check only validates synchronization health — it does not guarantee zero data loss. Always verify the report after the operation.
Primary SQL Server instance of the Distributed AG. Accepts pipeline input.
-AvailabilityGroupName
String
—
Required. Name of the Distributed Availability Group.
-SqlCredential
PSCredential
—
Optional SQL authentication credentials.
-OutputPath
String
C:\System\WinSrvLog\MSSQL
Directory for the failover report. Created if missing.
-Force
Switch
$false
Skip the ShouldProcess confirmation prompt.
-Rollback
Switch
$false
Fail back to the original primary. Skips the readiness check.
-EnableException
Switch
$false
Throw exceptions immediately.
Return Value
Property
Description
Status
SUCCESS, CANCELLED, or FAILED.
Direction
FAILOVER or ROLLBACK.
PrimaryAg
Name of the Distributed AG that was failed over.
SecondaryAg
Secondary AG name discovered from the cluster.
SecondaryServer
SQL instance that was the secondary before the failover.
PostFailoverStatus
Query result showing replica roles and sync health after the failover.
ReportFile
Path to the TXT failover report.
Examples
# Failover with confirmation promptInvoke-sqmDistributedFailover-SqlInstance"SQL01" `
-AvailabilityGroupName"MyDAG"# Failover without prompt (automated/scripted)Invoke-sqmDistributedFailover-SqlInstance"SQL01" `
-AvailabilityGroupName"MyDAG"-Force# Dry run (shows plan without executing)Invoke-sqmDistributedFailover-SqlInstance"SQL01" `
-AvailabilityGroupName"MyDAG"-WhatIf# Rollback to original primary after a failed migrationInvoke-sqmDistributedFailover-SqlInstance"SQL01" `
-AvailabilityGroupName"MyDAG"-Rollback-Force# Check result$r = Invoke-sqmDistributedFailover-SqlInstance"SQL01"-AvailabilityGroupName"MyDAG"-Force$r.PostFailoverStatus | Format-Tablenotepad$r.ReportFile