Created on

Azure SQL Managed Instance Link – Useful PowerShell commands


If you work with Azure SQL Managed Instance and you have configured Managed Link to a secondary replica (SQL Server e.g.), these PowerShell commands could be useful for you to know and use.

Get copy status for a database replica


# If commands are not found, use below command to install Azure PowerShell module
# Install-Module -Name Az

# Connect to Azure with account that has read access to SQL Managed Instance
Connect-AzAccount -SubscriptionId ""

$ManagedInstanceName = "sqlmi-prod-weu" # The name of your SQL Managed Instance
$DAGName = "sqlmi-proddb" # distributed availability group name - this is the name you defined for your Managed Instance Link on creation.

# Get resource group name 
$ResourceGroupName = (Get-AzSqlInstance -InstanceName $ManagedInstanceName).ResourceGroupName

# Inform about field to look for status in, to console
write-host "look for status in field: ReplicaState"

# Show link state details 
(Get-AzSqlInstanceLink -ResourceGroupName $ResourceGroupName -InstanceName $ManagedInstanceName -Name $DAGName).Databases

Remove database replica

# If commands are not found, use below command to install Azure PowerShell module
# Install-Module -Name Az

# Connect to Azure with account that has read access to SQL Managed Instance
Connect-AzAccount -SubscriptionId ""

$ManagedInstanceName = "sqlmi-prod-weu" # The name of your SQL Managed Instance
$DAGName = "sqlmi-proddb" # distributed availability group name - this is the name you defined for your Managed Instance Link on creation.

# Get resource group name 
$ResourceGroupName = (Get-AzSqlInstance -InstanceName $ManagedInstanceName).ResourceGroupName

# After removal of the link on the secondary replika, you have to remove it on the SQL Managed Instance
Remove-AzSqlInstanceLink -ResourceGroupName $ResourceGroupName -InstanceName $ManagedInstanceName -Name $DAGName