Switch-SqlAvailabilityGroup

Initiates a failover of an availability group to a specific secondary replica.
Switch-SqlAvailabilityGroup [[-Path] <string>] [-AllowDataLoss] [-Force] [-Script] [-Confirm] [-WhatIf][<CommonParameters>]
Switch-SqlAvailabilityGroup [-InputObject*] <AvailabilityGroup> [-AllowDataLoss] [-Force] [-Script] [-Confirm][-WhatIf] [<CommonParameters>]

The Switch-SqlAvailabilityGroup cmdlet initiates a failover of an availability group to a specific secondary replica. This cmdlet can be executed only on the target secondary replica. After the failover, the secondary replica to which you failed over becomes the new primary replica.

Parameters
-AllowDataLoss [<SwitchParameter>]

Initiates a forced failover to the target secondary replica. Data loss is possible. Therefore, you will be prompted for confirmation unless you specify the -Force or -Script parameter.

-Force [<SwitchParameter>]

Specify -Force with -AllowDataLoss to initiate a forced failover without confirmation.

-InputObject <AvailabilityGroup>

  • This value is required
  • Accepts pipeline input ByValue

The AvailabilityGroup object representing the availability group that will be failed over.

-Path <string>

The path to the availability group. This is an optional parameter. If not specified, the value of this parameter defaults to the current working location.

-Script [<SwitchParameter>]

Outputs a Transact-SQL script that performs the task executed by this cmdlet.

-Confirm [<SwitchParameter>]

Prompts you for confirmation before executing the command.

-WhatIf [<SwitchParameter>]

Describes what would happen if you executed the command without actually executing the command.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs

Microsoft.SqlServer.Management.Smo.AvailabilityGroup

Specifies the availability group to fail over to.

Examples
  1. This command performs a planned manual failover (without data loss) of the availability group ‘MyAg’ to the server instance named ‘SecondaryServerInstanceName’:
    C:PS> Switch-SqlAvailabilityGroup -Path SQLSERVER:SqlSecondaryServerInstanceNameAvailabilityGroupsMyAg
    

    This command must be executed against the server instance that hosts the secondary replica to which you are failing over.

  2. This command performs a forced failover (with possible data loss) of the availability group ‘MyAg’ to the server instance named ‘SecondaryServerInstanceName’:
    C:PS> Switch-SqlAvailabilityGroup -Path SQLSERVER:SqlSecondaryServerInstanceNameAvailabilityGroupsMyAg -AllowDataLoss
    

    Because a forced failover might result in data loss, you will be prompted to confirm this operation. This command must be executed against the server instance that hosts the secondary replica to which you are failing over.

  3. This command performs a forced failover (with possible data loss) of the availability group ‘MyAg’ to the server instance named ‘SecondaryServerInstanceName’:
    C:PS> Switch-SqlAvailabilityGroup -Path SQLSERVER:SqlComputerInstanceAvailabilityGroupsAgName -AllowDataLoss -Force
    

    The -Force option suppresses confirmation of this operation. Use the -Force option with caution, because a forced failover might result in the loss of data from databases participating the availability group. This command must be executed against the server instance that hosts the secondary replica to which you are failing over.

  4. This command outputs the Transact-SQL script that performs a planned manual failover of the availability group ‘MyAg’ to the server instance named ‘SecondaryServerInstanceName’:
    C:PS> Switch-SqlAvailabilityGroup -Path SQLSERVER:SqlSecondaryServerInstanceNameAvailabilityGroupsMyAg -Script
    

    Note that this command does not cause a failover.

Additional Notes