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

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

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

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

我找到了解决此问题的合理方法,即为每个数据库额外调用一次。 commandlet 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
    }
}