I wrote a PowerShell script to automate the copying of a database using Azure Automation. It had worked fine for a couple of months but there has been some strange behavior related to the Get-AzureSqlDatabaseCopy command. I was using the Get-AzureSqlDatabaseCopy to check whether the database copy has completed before moving onto the next step in the process. If the database copy hasn't completed in time then it will cause an error.
$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)
This code has worked fine in the past and the database copy normally takes 30-40 seconds to complete. There looks to be a problem with
$check = Get-AzureSqlDatabaseCopy -ServerName $SqlServer -DatabaseName $DatabaseDestination
$check does not look like it is being set to anything so $i is null and the loop does not work. This is something that only started happening in the last week. I went through some of the job records and it had been working fine until Thursday. I ran some test database copies today and $check is consistently not being set.
Does anyone know a reason for this change in behavior? Is there a better way to monitor a database copy using PowerShell in Azure Automation?