使用 Azure 自动化复制数据库时,Get-AzureSqlDatabaseCopy 未返回预期值

Get-AzureSqlDatabaseCopy not returning expected value when copying database using Azure Automation

我编写了一个 PowerShell 脚本来使用 Azure Automation 自动复制数据库。它在几个月内运行良好,但出现了一些与 Get-AzureSqlDatabaseCopy 命令相关的 st运行ge 行为。在进入流程的下一步之前,我使用 Get-AzureSqlDatabaseCopy 检查数据库复制是否已完成。如果数据库副本未及时完成,则会导致错误。

$SqlServer = 'servername.database.windows.net'
$DatabaseSource = 'Database1'
$DatabaseDestination = 'Database2'

Write-Output "Starting database copy - $DatabaseDestination"
Start-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseSource -PartnerServer $SqlServer -PartnerDatabase $DatabaseDestination    

$i = 0
$secs = 0
do
{
    $check = Get-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseDestination
    $i = $check.PercentComplete
    Write-Output "Database Copy ($DatabaseDestination) not complete in $secs seconds"

    $secs += 10
    Start-Sleep -s 10
}
while($i -ne $null -and $secs -lt 600)

此代码过去运行良好,数据库复制通常需要 30-40 秒才能完成。

似乎有问题
$check = Get-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseDestination

$check 看起来没有设置任何值,因此 $i 为 null 并且循环不起作用。这是上周才开始发生的事情。我查看了一些工作记录,直到星期四它一直工作正常。我今天 运行 一些测试数据库副本,并且一直没有设置 $check。

有人知道这种行为变化的原因吗?在 Azure Automation 中使用 PowerShell 监视数据库副本是否有更好的方法?

下面检查副本是否存在是在目标端完成的:

$check = Get-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseDestination

不保证在 Start-AzureSqlDtaabaseCopy 之前创建目标端复制对象 returns,因此 $check 过早变为 null,这意味着复制实际上并未完成。

要解决此问题,我们建议检查源端是否存在副本:

$check = Get-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseSource -PartnerDatabase $DatabaseDestination