Home
%3CLINGO-SUB%20id%3D%22lingo-sub-368988%22%20slang%3D%22en-US%22%3ESchedule%20Data%20Sync%20with%20Azure%20SQL%20Database%20use%20an%20Automation%20account%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-368988%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Mar%2004%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-sync-data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Azure%20Data%20Sync%20(in-preview)%20%3C%2FA%3E%20%2C%20is%20one%20of%20the%20common%20Azure%20SQL%20DB%20migration%2Freplication%20features%20introduced%20with%20Azure%20SQL%20DB%2C%20one%20of%20the%20common%20question%20received%20regarding%20Azure%20Data%20Sync%20is%20about%20running%20a%26nbsp%3BSync%20based%20on%20a%20schedule%2C%20is%20there%20any%20option%20to%20setup%20a%20customizable%20schedule%20can%20run%20according%20to%20start%2Fend%20time%20user%20can%20specify%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20At%20the%20moment%2C%20by%20design%20Azure%20Data%20Sync%20can%20be%20setup%20with%202%20types%20of%20schedules%2C%20were%20the%20first%20one%20is%20automatic%20schedule%2C%20the%20automatic%20schedule%20works%20based%20on%20time%20intervals%2C%20the%20lowest%20value%20is%205%20minutes.%20The%20second%20type%20is%20manual%20schedule%2C%20and%20in%20this%20type%20it%26nbsp%3Bleft%20for%20the%20user%20to%20run%20the%20sync%20whenever%20they%26nbsp%3Bneed%20through%20the%20Azure%20Portal%20or%20via%20PowerShell.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20From%20the%20above%20we%20figured%20the%20need%20to%20build%20this%20PowerShell%20to%20automate%20the%20sync%20based%20on%26nbsp%3Ba%20start%20and%20end%20time%20defined%20by%20the%20user.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20idea%20behind%20this%20PowerShell%20workflow%20script%20is%26nbsp%3Bto%20trigger%20a%20one%20time%20sync%20manually%2C%20if%20it%20succeeds%2C%20switch%20to%20automatic%20sync%20and%20set%20Sync%20time%20intervals%20according%20to%20your%20preferences%2C%20in%20the%20PowerShell%20workflow%20script%20set%20it%20to%20run%20based%20on%20300%20seconds%20intervals.%20Then%20wait%20until%20Sync%20is%20competed%20successfully%20and%20set%20Sync%20back%20to%20Off%20and%20stop%20Sync%20after%20wait%20for%20a%20XX%20time%20you%20specified%2C%20(in%20our%20test%20we%20set%20the%20time%20to%2010%20minutes).%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CB%3EPrerequisites%3A%20%3C%2FB%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20complete%20this%20exercise%2C%20you%20should%20have%20the%20following%20resources%20in%20place%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Azure%20SQL%20DB%2C%20and%20Data%20Sync%20group%20created%2C%20either%20to%20sync%20between%20on-premises%20database%20and%2For%20Azure%20SQL%20DB%2C%20for%20more%20reference%20how%20to%20create%20Azure%20SQL%20DB%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-get-started-sql-data-sync%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20%2C%20to%20create%20a%20new%20Data%20Sync%20group%20from%20here%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20create%20an%20automation%20account%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20Go%20to%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fportal.azure.com%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20Select%20All%20services%20and%20type%20%E2%80%9C%20%3CB%3E%20automation%20%3C%2FB%3E%20%E2%80%9D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88981iFB49A11A68F506A3%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%201%20%E2%80%93%20search%20for%20automation%20account%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203)%20Click%20%E2%80%9C%2B%20%3CB%3E%20Add%20%3C%2FB%3E%20%E2%80%9D%20to%20create%20a%20new%20Automation%20Account%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204)%20Fill%20in%20the%20required%20details%20for%20your%20automation%20account%2C%20and%20make%20sure%20%E2%80%9CCreate%20Azure%20Run%20As%20account%E2%80%9D%20set%20to%20Yes%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88982iACE4EEAA140B00DA%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%202%20%E2%80%93%20create%20a%20new%20automation%20account%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205)%20Click%20%E2%80%9C%20%3CB%3E%20create%20%3C%2FB%3E%20%E2%80%9D.%20The%20new%20automation%20created%2C%20and%20configuration%20blade%20will%20be%20opened%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206)%20First%2C%20make%20sure%20%22%20%3CB%3E%20AzureClassicRunAsConnection%20%3C%2FB%3E%20%22%20and%20%22%20%3CB%3E%20AzureRunAsConnection%20%3C%2FB%3E%20%E2%80%9D%20connections%20created%20automatically%20during%20Automation%20account%20creation.%20Note%3A%20(%22%20%3CB%3E%20AzureRunAsConnection%20%3C%2FB%3E%20%22%20connection%20will%20be%20used%20in%20our%20PowerShell%20workflow%20script%20later%20to%20authenticate%20to%20Azure%20subscription)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88983i6326257AB06C88C4%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%203%20%E2%80%93%20confirm%20connections%20are%20created%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207)%20Second%2C%20verify%20that%20your%20Automation%20account%20has%20latest%20PowerShell%20modules%20imported%20and%20are%20up%20to%20date%20(%20%3CSTRONG%3E%20AzureRM.Automation%20%3C%2FSTRONG%3E%20-%20%3CSTRONG%3E%20AzureRM.Profile%20%3C%2FSTRONG%3E%20-%20%3CSTRONG%3E%20AzureRM.Sql%20%3C%2FSTRONG%3E%20)%20(tested%20available%20Version%3A%20%3CSTRONG%3E%203.4.0%20%3C%2FSTRONG%3E%20)%20and%20Azure%20to%20Version%3A%20%3CSTRONG%3E%205.1.1%20%3C%2FSTRONG%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88984i511AAB2E4D1A8C18%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%204%20%E2%80%93%20confirm%20modules%20are%20up%20to%20date%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208)%3C%2Fimg%3E%20Import%20PowerShell%20script%20%3CB%3E%20Update-ModulesInAutomationToLatestVersion%20%3C%2FB%3E%20from%20runbook%20gallery%20and%20make%20sure%20it%20is%20published%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CB%3E%20Note%3A%20(without%20this%20PowerShell%20script%20the%20Export%20PowerShell%20workflow%20will%20not%20work)!%20%3C%2FB%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20import%20%3CB%3E%20Update-ModulesInAutomationToLatestVersion%20%3C%2FB%3E%20PowerShell%20script%20from%20runbook%20Gallery%2C%20follow%20below%20steps%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20a.%20Go%20to%20automation%20account%20b.%20Select%20runbooks%20from%20middle%20blade%20c.%20From%20top%20bar%20select%20Browse%20gallery%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20d.%20Set%20the%20filter%20Gallery%20Source%20to%20PowerShell%20Gallery%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20e.%20Search%20for%20%3CB%3E%20Update-ModulesInAutomationToLatestVersion%20%3C%2FB%3E%20PowerShell%20script%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20f.%20Select%20this%20script%20and%20import%20it%20g.%20Revert%20to%20runbooks%20again%20h.%20Select%20recently%20imported%20PowerShell%20script%20i.%20From%20top%20bar%20select%20Edit%2C%20then%20Publish%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20j.%20Verify%20that%20this%20script%20in%20Published%20state%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20we%20will%20import%20the%20PowerShell%20workflow%20script!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20From%20Automation%20account%20overview%20dashboard%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20Select%20runbooks%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203)%20Click%20Add%20runbook%20--%26gt%3B%20Create%20a%20new%20runbook%20(make%20sure%20you%20select%20PowerShell%20Workflow!)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204)%20Click%20%E2%80%98create%E2%80%9D.%20Once%20new%20runbook%20created%2C%20it%20will%20present%20in%20runbooks%20in%20new%20state!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205)%20Select%20%3CB%3E%20Schedule-DataSync%20%3C%2FB%3E%20and%20from%20top%20bar%20click%20Edit%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206)%20Copy%20and%20Past%20the%20PowerShell%20script%20to%20the%20new%20runbook%20and%20Save%2C%20then%20publish%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207)%20Click%20Edit%2C%20and%20before%20click%20%E2%80%9C%20%3CB%3E%20Test%20Pane%20%3C%2FB%3E%20%E2%80%9D%20make%20sure%20you%20update%20required%20parameters%20(Subscription%20ID%20%E2%80%93%20Resource%20Group%20Name%20%E2%80%93%20Server%20name%20%E2%80%93%20Database%20name%20%E2%80%93%20Sync%20Group%20name%20%E2%80%93%20Time%20Interval%20in%20seconds)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208)%3C%2Fimg%3E%20You%20can%20review%20completed%20jobs%20from%20runbooks%20jobs%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20schedule%20follow%20below%20steps%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20Click%20on%20schedule%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88985iCD410781D2F4E0A6%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%205%20%E2%80%93%20schedules%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20Click%20on%20%E2%80%9C%20%3CB%3E%20Create%20a%20new%20schedule%20%3C%2FB%3E%20%E2%80%9D%20and%20fill%20in%20the%20required%20information%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88986iFC381AD04CCEF1A2%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%206%20%E2%80%93%20Create%20new%20schedule%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Monitor%20the%20jobs%20status%20form%20here%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88987iC5ECEBC829C8D1CF%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%207%20%E2%80%93%20Automation%20account%20Monitoring%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Test%20pane%20sample%20completed%20script%20run%20view%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88988i7E808AC7B170CA57%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%208%20%E2%80%93%20Test%20pane%20completed%20run%20script%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F88989iBC3B06F8D0879368%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Figure%209%20%E2%80%93%20monitor%20jobs%20output%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22PowerShell%22%5D%20%3CBR%20%2F%3E%20workflow%20Schedule-DataSync%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20inlineScript%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24connectionName%20%3D%20%22AzureRunAsConnection%22%20%3CBR%20%2F%3E%20try%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Get%20the%20connection%20%22AzureRunAsConnection%20%22%20%3CBR%20%2F%3E%20%24servicePrincipalConnection%20%3D%20Get-AutomationConnection%20-Name%20%24connectionName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%22Login%20to%20Azure%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add-AzureRmAccount%20%60%20%3CBR%20%2F%3E%20-ServicePrincipal%20%60%20%3CBR%20%2F%3E%20-TenantId%20%24servicePrincipalConnection.TenantId%20%60%20%3CBR%20%2F%3E%20-ApplicationId%20%24servicePrincipalConnection.ApplicationId%20%60%20%3CBR%20%2F%3E%20-CertificateThumbprint%20%24servicePrincipalConnection.CertificateThumbprint%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20catch%20%7B%20%3CBR%20%2F%3E%20if%20(!%24servicePrincipalConnection)%20%7B%20%3CBR%20%2F%3E%20%24ErrorMessage%20%3D%20%22Connection%20%24connectionName%20not%20found.%22%20%3CBR%20%2F%3E%20throw%20%24ErrorMessage%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%7B%20%3CBR%20%2F%3E%20Write-Error%20-Message%20%24_.Exception%20%3CBR%20%2F%3E%20throw%20%24_.Exception%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Sync%20Group%20to%20start%20%3CBR%20%2F%3E%20%24SubscriptionId%20%3D%20%22xxxxx-xxxxx-xxxxx-xxxxxx-xxxxxx%22%20%3CBR%20%2F%3E%20%24ResourceGroupName%20%3D%20%22ResourceGroup%22%20%3CBR%20%2F%3E%20%24ServerName%20%3D%20%22ServerName%22%20%3CBR%20%2F%3E%20%24DatabaseName%20%3D%20%22DatabaseName%22%20%3CBR%20%2F%3E%20%24SyncGroupName%20%3D%20%22SyncGroup%22%20%3CBR%20%2F%3E%20%24IntervalInSeconds%20%3D%20%22300%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Trigger%20sync%20manually%20%3CBR%20%2F%3E%20Write-Output%20%22Trigger%20sync%20manually%22%20%3CBR%20%2F%3E%20%24SyncLogStartTime%20%3D%20Get-Date%20%3CBR%20%2F%3E%20Start-AzureRmSqlSyncGroupSync%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-SyncGroupName%20%24SyncGroupName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Check%20the%20sync%20log%20and%20wait%20until%20the%20first%20sync%20succeeded%20%3CBR%20%2F%3E%20Write-Output%20%22Check%20the%20sync%20log%22%20%3CBR%20%2F%3E%20%24IsSucceeded%20%3D%20%24false%20%3CBR%20%2F%3E%20For%20(%24i%20%3D%200%3B%20(%24i%20-lt%20300)%20-and%20(-not%20%24IsSucceeded)%3B%20%24i%20%3D%20%24i%20%2B%2010)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Start-Sleep%20-s%2010%20%3CBR%20%2F%3E%20%24SyncLogEndTime%20%3D%20Get-Date%20%3CBR%20%2F%3E%20%24SyncLogList%20%3D%20Get-AzureRmSqlSyncGroupLog%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-SyncGroupName%20%24SyncGroupName%20%60%20%3CBR%20%2F%3E%20-StartTime%20%24SyncLogStartTime.ToUniversalTime()%20%60%20%3CBR%20%2F%3E%20-EndTime%20%24SyncLogEndTime.ToUniversalTime()%20%3CBR%20%2F%3E%20if%20(%24SynclogList.Length%20-gt%200)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20foreach%20(%24SyncLog%20in%20%24SyncLogList)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(%24SyncLog.Details.Contains(%22Sync%20completed%20successfully%22))%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Host%20%24SyncLog.TimeStamp%20%3A%20%24SyncLog.Details%20%3CBR%20%2F%3E%20%24IsSucceeded%20%3D%20%24true%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if%20(%24IsSucceeded)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%23%20Enable%20scheduled%20sync%20%3CBR%20%2F%3E%20Write-Output%20%22Enable%20the%20scheduled%20sync%20with%20300%20seconds%20interval%22%20%3CBR%20%2F%3E%20Update-AzureRmSqlSyncGroup%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-Name%20%24SyncGroupName%20%60%20%3CBR%20%2F%3E%20-IntervalInSeconds%20%24IntervalInSeconds%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%23%20Output%20all%20log%20if%20sync%20doesn't%20succeed%20in%20300%20seconds%20%3CBR%20%2F%3E%20%24SyncLogEndTime%20%3D%20Get-Date%20%3CBR%20%2F%3E%20%24SyncLogList%20%3D%20Get-AzureRmSqlSyncGroupLog%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-SyncGroupName%20%24SyncGroupName%20%60%20%3CBR%20%2F%3E%20-StartTime%20%24SyncLogStartTime.ToUniversalTime()%20%60%20%3CBR%20%2F%3E%20-EndTime%20%24SyncLogEndTime.ToUniversalTime()%20%3CBR%20%2F%3E%20if%20(%24SynclogList.Length%20-gt%200)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20foreach%20(%24SyncLog%20in%20%24SyncLogList)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Host%20%24SyncLog.TimeStamp%20%3A%20%24SyncLog.Details%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20Write-Output%20%22This%20Script%20will%20disable%20auto%20sync%20and%20stop%20sync%20in%2010%20minutes%22%20%3CBR%20%2F%3E%20Start-Sleep%20-s%20600%20%3CBR%20%2F%3E%20%23%20Stop%20sync%20manually%20%3CBR%20%2F%3E%20Write-Output%20%22Set%20automatic%20sync%20OFF%22%20%3CBR%20%2F%3E%20Update-AzureRmSqlSyncGroup%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-Name%20%24SyncGroupName%20%60%20%3CBR%20%2F%3E%20-IntervalInSeconds%20%22-1%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Stop%20sync%20%3CBR%20%2F%3E%20Write-Output%20%22Stop%20sync%22%20%3CBR%20%2F%3E%20Stop-AzureRmSqlSyncGroupSync%20-ResourceGroupName%20%24ResourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20%60%20%3CBR%20%2F%3E%20-SyncGroupName%20%24SyncGroupName%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20hope%20you%20enjoy%20run%20this%20runbook%20PowerShell%20script%2C%20please%20share%20any%20thoughts%20here%20in%20post%20comments.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EMore%20references%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-offering-get-started%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Getting%20Started%20with%20Azure%20Automation%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CA%20href%3D%22http%3A%2F%2FCreate%20a%20standalone%20Azure%20Automation%20account%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Create%20a%20standalone%20Azure%20Automation%20account%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-368988%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Mar%2004%2C%202018%20Azure%20Data%20Sync%20(in-preview)%2C%20is%20one%20of%20the%20common%20Azure%20SQL%20DB%20migration%2Freplication%20features%20introduced%20with%20Azure%20SQL%20DB%2C%20one%20of%20the%20common%20question%20received%20regarding%20Azure%20Data%20Sync%20is%20about%20running%20a%26nbsp%3BSync%20based%20on%20a%20schedule%2C%20is%20there%20any%20option%20to%20setup%20a%20customizable%20schedule%20can%20run%20according%20to%20start%2Fend%20time%20user%20can%20specify%3FAt%20the%20moment%2C%20by%20design%20Azure%20Data%20Sync%20can%20be%20setup%20with%202%20types%20of%20schedules%2C%20were%20the%20first%20one%20is%20automatic%20schedule%2C%20the%20automatic%20schedule%20works%20based%20on%20time%20intervals%2C%20the%20lowest%20value%20is%205%20minutes.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-368988%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eautomation%20account%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%20runbook%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
First published on MSDN on Mar 04, 2018
Azure Data Sync (in-preview) , is one of the common Azure SQL DB migration/replication features introduced with Azure SQL DB, one of the common question received regarding Azure Data Sync is about running a Sync based on a schedule, is there any option to setup a customizable schedule can run according to start/end time user can specify?

At the moment, by design Azure Data Sync can be setup with 2 types of schedules, were the first one is automatic schedule, the automatic schedule works based on time intervals, the lowest value is 5 minutes. The second type is manual schedule, and in this type it left for the user to run the sync whenever they need through the Azure Portal or via PowerShell.

From the above we figured the need to build this PowerShell to automate the sync based on a start and end time defined by the user.

The idea behind this PowerShell workflow script is to trigger a one time sync manually, if it succeeds, switch to automatic sync and set Sync time intervals according to your preferences, in the PowerShell workflow script set it to run based on 300 seconds intervals. Then wait until Sync is competed successfully and set Sync back to Off and stop Sync after wait for a XX time you specified, (in our test we set the time to 10 minutes).

Prerequisites:

To complete this exercise, you should have the following resources in place:

Azure SQL DB, and Data Sync group created, either to sync between on-premises database and/or Azure SQL DB, for more reference how to create Azure SQL DB from here , to create a new Data Sync group from here

To create an automation account:

1) Go to https://portal.azure.com

2) Select All services and type “ automation



Figure 1 – search for automation account

3) Click “+ Add ” to create a new Automation Account

4) Fill in the required details for your automation account, and make sure “Create Azure Run As account” set to Yes



Figure 2 – create a new automation account

5) Click “ create ”. The new automation created, and configuration blade will be opened

6) First, make sure " AzureClassicRunAsConnection " and " AzureRunAsConnection ” connections created automatically during Automation account creation. Note: (" AzureRunAsConnection " connection will be used in our PowerShell workflow script later to authenticate to Azure subscription)



Figure 3 – confirm connections are created

7) Second, verify that your Automation account has latest PowerShell modules imported and are up to date ( AzureRM.Automation - AzureRM.Profile - AzureRM.Sql ) (tested available Version: 3.4.0 ) and Azure to Version: 5.1.1 .



Figure 4 – confirm modules are up to date

8) Import PowerShell script Update-ModulesInAutomationToLatestVersion from runbook gallery and make sure it is published

Note: (without this PowerShell script the Export PowerShell workflow will not work)!

To import Update-ModulesInAutomationToLatestVersion PowerShell script from runbook Gallery, follow below steps:

a. Go to automation account

b. Select runbooks from middle blade

c. From top bar select Browse gallery

d. Set the filter Gallery Source to PowerShell Gallery

e. Search for Update-ModulesInAutomationToLatestVersion PowerShell script

f. Select this script and import it

g. Revert to runbooks again

h. Select recently imported PowerShell script

i. From top bar select Edit, then Publish

j. Verify that this script in Published state

Now we will import the PowerShell workflow script!

1) From Automation account overview dashboard

2) Select runbooks

3) Click Add runbook --> Create a new runbook (make sure you select PowerShell Workflow!)

4) Click ‘create”. Once new runbook created, it will present in runbooks in new state!

5) Select Schedule-DataSync and from top bar click Edit

6) Copy and Past the PowerShell script to the new runbook and Save, then publish

7) Click Edit, and before click “ Test Pane ” make sure you update required parameters (Subscription ID – Resource Group Name – Server name – Database name – Sync Group name – Time Interval in seconds)

8) You can review completed jobs from runbooks jobs

To schedule follow below steps:

1) Click on schedule



Figure 5 – schedules

2) Click on “ Create a new schedule ” and fill in the required information



Figure 6 – Create new schedule

Monitor the jobs status form here



Figure 7 – Automation account Monitoring

Test pane sample completed script run view



Figure 8 – Test pane completed run script



Figure 9 – monitor jobs output

[code language="PowerShell"]
workflow Schedule-DataSync {

inlineScript
{
$connectionName = "AzureRunAsConnection"
try {

# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName

"Login to Azure"

Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}

catch {
if (!$servicePrincipalConnection) {
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
}
else {
Write-Error -Message $_.Exception
throw $_.Exception
}
}

# Sync Group to start
$SubscriptionId = "xxxxx-xxxxx-xxxxx-xxxxxx-xxxxxx"
$ResourceGroupName = "ResourceGroup"
$ServerName = "ServerName"
$DatabaseName = "DatabaseName"
$SyncGroupName = "SyncGroup"
$IntervalInSeconds = "300"

# Trigger sync manually
Write-Output "Trigger sync manually"
$SyncLogStartTime = Get-Date
Start-AzureRmSqlSyncGroupSync -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-SyncGroupName $SyncGroupName

# Check the sync log and wait until the first sync succeeded
Write-Output "Check the sync log"
$IsSucceeded = $false
For ($i = 0; ($i -lt 300) -and (-not $IsSucceeded); $i = $i + 10)
{
Start-Sleep -s 10
$SyncLogEndTime = Get-Date
$SyncLogList = Get-AzureRmSqlSyncGroupLog -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-SyncGroupName $SyncGroupName `
-StartTime $SyncLogStartTime.ToUniversalTime() `
-EndTime $SyncLogEndTime.ToUniversalTime()
if ($SynclogList.Length -gt 0)
{
foreach ($SyncLog in $SyncLogList)
{
if ($SyncLog.Details.Contains("Sync completed successfully"))
{
Write-Host $SyncLog.TimeStamp : $SyncLog.Details
$IsSucceeded = $true
}
}
}
}

if ($IsSucceeded)
{
# Enable scheduled sync
Write-Output "Enable the scheduled sync with 300 seconds interval"
Update-AzureRmSqlSyncGroup -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-Name $SyncGroupName `
-IntervalInSeconds $IntervalInSeconds
}
else
{
# Output all log if sync doesn't succeed in 300 seconds
$SyncLogEndTime = Get-Date
$SyncLogList = Get-AzureRmSqlSyncGroupLog -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-SyncGroupName $SyncGroupName `
-StartTime $SyncLogStartTime.ToUniversalTime() `
-EndTime $SyncLogEndTime.ToUniversalTime()
if ($SynclogList.Length -gt 0)
{
foreach ($SyncLog in $SyncLogList)
{
Write-Host $SyncLog.TimeStamp : $SyncLog.Details
}
}
}
Write-Output "This Script will disable auto sync and stop sync in 10 minutes"
Start-Sleep -s 600
# Stop sync manually
Write-Output "Set automatic sync OFF"
Update-AzureRmSqlSyncGroup -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-Name $SyncGroupName `
-IntervalInSeconds "-1"

# Stop sync
Write-Output "Stop sync"
Stop-AzureRmSqlSyncGroupSync -ResourceGroupName $ResourceGroupName `
-ServerName $ServerName `
-DatabaseName $DatabaseName `
-SyncGroupName $SyncGroupName
}
}[/code]

I hope you enjoy run this runbook PowerShell script, please share any thoughts here in post comments.

More references:
Getting Started with Azure Automation
Create a standalone Azure Automation account