Sam H
Sam H

Reputation: 879

How to determine SQL database replication roles using the Azure PowerShell command Get-AzureRMSqlDatabase

Using the Azure Resource Manager PowerShell commands, is there a simple way to tell whether a database is involved in geo-replication role as either a Primary or Secondary? I used to read the Status property returned by Get-AzureSqlDatabase, and a value of 0 meant that the database was Primary. However, there is no corresponding property returned by Get-AzureRMSqlDatabase; it still returns a status column, but the value is "Online" for both primary and secondary databases.

The reason I need this is that I'm trying to maintain dozens of databases across multiple subscriptions and servers, and I am trying to automate actions that should only be taken on the primary databases.

Upvotes: 1

Views: 374

Answers (1)

Sam H
Sam H

Reputation: 879

I found a reasonable solution to this problem, making one extra call per database. The commandlet Get-AzureRmSqlDatabaseReplicationLink does exactly what I needed, with one caveat; I know that I'm not supposed to be passing the same value as both ResourceGroupName and PartnerResourceGroupName, but it seems to work (at least for now), so I'm going with it to avoid having to make one call per resource group in the subscription.

Using that, I was able to create this simple function:

Function IsSecondarySqlDatabase {
    # This function determines whether specified database is performing a secondary replication role.
    # You can use the Get-AzureRMSqlDatabase command to get an instance of a [Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel] object.
    param
    (
        [Microsoft.Azure.Commands.Sql.Database.Model.AzureSqlDatabaseModel] $SqlDB
    )
    process {
        $IsSecondary = $false;
        $ReplicationLinks = Get-AzureRmSqlDatabaseReplicationLink `
            -ResourceGroupName $SqlDB.ResourceGroupName `
            -ServerName $SqlDB.ServerName `
            -DatabaseName $SqlDB.DatabaseName `
            -PartnerResourceGroupName $SqlDB.ResourceGroupName
        $ReplicationLinks | ForEach-Object -Process `
        {
            if ($_.Role -ne "Primary")
            {
                $IsSecondary = $true
            }
        }
        return $IsSecondary
    }
}

Upvotes: 2

Related Questions