如何使用Azure PowerShell命令Get-AzureRMSqlDatabase确定SQL数据库复制角色



使用Azure资源管理器PowerShell命令,是否有一种简单的方法来判断数据库是作为主数据库还是从数据库参与地理复制角色?我过去常常读取Get-AzureSqlDatabase返回的Status属性,值为0表示数据库为Primary。但是,Get-AzureRMSqlDatabase没有返回相应的属性;它仍然返回一个状态列,但是主数据库和从数据库的值都是"Online"。

我需要这个的原因是,我正试图在多个订阅和服务器上维护数十个数据库,并且我正试图自动执行应该只在主数据库上执行的操作。

我找到了一个合理的解决方案,每个数据库多调用一次。命令let Get-AzureRmSqlDatabaseReplicationLink做了我所需要的,有一个警告;我知道我不应该传递与ResourceGroupName和PartnerResourceGroupName相同的值,但它似乎可以工作(至少现在),所以我将使用它来避免必须在订阅中对每个资源组进行一次调用。

使用它,我能够创建这个简单的函数:

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
    }
}

最新更新