Home
%3CLINGO-SUB%20id%3D%22lingo-sub-830801%22%20slang%3D%22en-US%22%3EAutomate%20migration%20to%20SQL%20Managed%20Instance%20using%20Azure%20PowerShell%20and%20DbaTools%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-830801%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20Microsoft%20Azure%2C%20you%20can%20easily%20migrate%20your%20databases%20from%20SQL%20Server%20on-premises%20or%20Azure%20VMs%20to%20the%20fully-managed%20PaaS%20database%20service%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Fsql-database-managed-instance%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20SQL%20Database%20Managed%20Instance%3C%2FA%3E.%20In%20this%20post%20you%20will%20see%20how%20to%20create%20PowerShell%20scripts%20that%20automate%20migration%20to%20Managed%20Instance%20using%20Azure%20PowerShell%20and%20DbaTools.%3C%2FP%3E%0A%3CP%3EThe%20approach%20used%20in%20this%20article%20is%20%3CSTRONG%3EOFFLINE%3C%2FSTRONG%3Emigration.%20You%20should%20stop%20the%20activities%20on%20your%20source%20SQL%20Server%20instance%20while%20the%20databases%20and%20server-level%20objects%20are%20copied%20to%20the%20target%20Managed%20Instance.%20If%20you%20need%20ONLINE%20migration%20that%20will%20enable%20you%20to%20use%20your%20source%20databases%20while%20they%20are%20moving%20to%20Managed%20Instance%2C%20take%20a%20look%20at%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdms%2Ftutorial-sql-server-managed-instance-online%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Migration%20Service%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3ETypically%2C%20the%20steps%20involved%20in%20an%20offline%20migration%20process%20are%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20an%20Azure%20Blob%20Storage%20account%20that%20will%20be%20used%20to%20temporary%20hold%20the%20database%20backups%20that%20will%20be%20moved%20from%20SQL%20Server%20to%20Managed%20Instance.%3C%2FLI%3E%0A%3CLI%3EBack%20up%20the%20databases%20to%20Azure%20Blob%20Storage%20and%20restore%20them%20from%20Azure%20Blob%20Storage%20to%20Managed%20Instance.%3C%2FLI%3E%0A%3CLI%3EMigrate%20server-level%20objects%20such%20as%20logins%2C%20agent%20jobs%20from%20the%20source%20to%20destination%20instance.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20this%20article%2C%20I%20will%20use%20Azure%20PowerShell%20to%20create%20and%20manage%20necessary%20Azure%20resources%2C%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.dbatools.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDBATools%20PowerShell%3C%2FA%3Elibrary%20to%20initiate%20migration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3ENOTE%3A%20This%20procedure%20is%20based%20on%20the%20assumption%20that%20the%20machine%20where%20you%20are%20running%20this%20script%20on%20has%20access%20both%20to%20the%20source%20SQL%20Server%20and%20the%20target%20Managed%20Instance.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1819134312%22%20id%3D%22toc-hId-1819134312%22%3EPrerequisites%3C%2FH2%3E%0A%3CP%3EBefore%20you%20begin%2C%20you%20should%20make%20sure%20that%20you%20have%20installed%20all%20necessary%20PowerShell%20libraries.%20The%20following%20PowerShell%20modules%20are%20used%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EInstall-Module%20Az.Resources%0AInstall-Module%20Az.Storage%0AInstall-Module%20dbatools%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3EMake%20sure%20that%20you%20have%20the%20latest%26nbsp%3B%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E150.18147.0%3C%2FFONT%3Eor%20higher%20version%20of%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EMicrosoft.SqlServer.SqlManagementObjects%20installed%20or%20these%20commands%20will%20fail.%20If%20you%20don't%20install%20anything%2C%20DbaTools%20will%20include%20the%20latest%20version%20of%20SMO.%20However%2C%20if%20you%20have%20the%20older%20version%20and%20include%20it%20in%20the%20PowerShell%20session%2C%20the%20script%20will%20fail.%3C%2FFONT%3E%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--733022649%22%20id%3D%22toc-hId--733022649%22%3EParameter%20definition%3C%2FH2%3E%0A%3CP%3EAs%20a%20first%20step%2C%20populate%20the%20parameters%20that%20define%20the%20names%20of%20source%2Ftarget%20instances%20and%20temporary%20Azure%20Blob%20Storage%20account%20used%20in%20this%20sample%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20temporary%20resources%20needed%20for%20backups%0A%24location%20%3D%20%22westus%22%0A%24resourceGroup%20%3D%20%22temp-migration-demo-rg%22%0A%24blobStorageAccount%20%3D%20%22temp-migrationdemostorage%22%0A%24containerName%20%3D%20%22backups%22%0A%20%0A%23%20source%20and%20target%20instances%0A%24sourceInstance%20%3D%20%22SOURCESQLSERVER%22%0A%24sourceDatabase%20%3D%20%22WideWorldImporters%22%0A%20%0A%24targetInstance%20%3D%20%22targetmi.public.920d05d7463d.database.windows.net%2C3342%22%0A%24targetDatabase%20%3D%20%22WideWorldImporters%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EChange%20these%20parameters%20depending%20on%20your%20needs.%20In%20this%20sample%2C%20I%20am%20using%20a%20public%20endpoint%20to%20connect%20to%20Managed%20Instance.%20If%20you%20don%E2%80%99t%20have%20a%20public%20endpoint%2C%20you%20can%20set%20up%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Fsql-database-managed-instance-configure-p2s%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EPoint-to-site%20connection%3C%2FA%3Eor%20run%20the%20script%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Fsql-database-managed-instance-configure-vm%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20virtual%20machine%20that%20is%20placed%20in%20the%20same%20subnet%20as%20your%20Managed%20Instance%3C%2FA%3E.%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3EMake%20sure%20that%20you%20use%20the%20same%20region%20where%20your%20Managed%20Instance%20is%20placed%20because%20this%20will%20speed-up%20the%20database%20restore%20process.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1009787686%22%20id%3D%22toc-hId-1009787686%22%3EStorage%20setup%3C%2FH2%3E%0A%3CP%3EIn%20order%20to%20move%20your%20databases%2C%20you%20need%20to%20create%20a%20temporary%20Azure%20Blob%20Storage%20account.%20If%20you%20already%20have%20one%2C%20you%20can%20skip%20this%20step.%3C%2FP%3E%0A%3CP%3EThe%20following%20PowerShell%20script%20creates%20an%20Azure%20Blob%20Storage%20account%20that%20will%20be%20used%20in%20this%20sample%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ENew-AzResourceGroup%20-Name%20%24resourceGroup%20-Location%20%24location%0A%20%0A%24storageAccount%20%3D%20New-AzStorageAccount%20-ResourceGroupName%20%24resourceGroup%20%60%0A-Name%20%24blobStorageAccount%20%60%0A-Location%20%24location%20%60%0A-SkuName%20Standard_LRS%20%60%0A-Kind%20StorageV2%0A%20%0A%24ctx%20%3D%20%24storageAccount.Context%0ANew-AzStorageContainer%20-Name%20%24containerName%20-Context%20%24ctx%20-Permission%20Container%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20execute%20this%20script%2C%20you%20will%20have%20Azure%20Blob%20Storage%20account%20ready%20to%20be%20used%20in%20the%20migration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1542369275%22%20id%3D%22toc-hId--1542369275%22%3ESource%20instance%20setup%3C%2FH2%3E%0A%3CP%3EOnce%20you%20create%20an%20Azure%20Blob%20Storage%20account%2C%20you%20need%20to%20generate%20a%20SAS%20key%20that%20will%20enable%20your%20SQL%20Server%20instance%20to%20access%20the%20Azure%20Blob%20Storage%20account%20and%20put%20the%20database%20backups%20there.%20This%20SAS%20key%20will%20be%20represented%20as%20a%20Credential%20object%20in%20SQL%20Server%20instance%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%24sas%20%3D%20(New-AzStorageAccountSASToken%20-Service%20Blob%20-ResourceType%20Object%20-Permission%20%22rw%22%20-Context%20%24ctx).TrimStart('%3F')%0A%23%20Note%3A%20it%20must%20have%20r%20permission!%0A%20%0A%24sourceCred%20%3D%20New-DbaCredential%20-SqlInstance%20%24sourceInstance%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Name%20%22https%3A%2F%2F%24blobStorageAccount.blob.core.windows.net%2F%24containerName%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Identity%20%22SHARED%20ACCESS%20SIGNATURE%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-SecurePassword%20(ConvertTo-SecureString%20%24sas%20-AsPlainText%20-Force)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20you%20are%20ready%20to%20back%20up%20your%20SQL%20Server%20databases%20to%20Azure%20Blob%20Storage.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-200441060%22%20id%3D%22toc-hId-200441060%22%3EDatabase%20migration%3C%2FH2%3E%0A%3CP%3EWith%20the%20prepared%20Azure%20Blob%20Storage%20account%20and%20credentials%2C%20you%20can%20take%20the%20backups%20of%20you%20SQL%20Server%20databases%20and%20put%20them%20to%20Azure%20Blob%20Storage.%20I%20will%20assume%20that%20you%20have%20%3CSPAN%20style%3D%22color%3A%20%23000000%3B%20font-family%3A%20'Segoe%20UI'%2C%20SegoeUI%2C%20'Segoe%20WP'%2C%20'Helvetica%20Neue'%2C%20Helvetica%2C%20Tahoma%2C%20Arial%2C%20sans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant-ligatures%3A%20normal%3B%20font-variant-caps%3A%20normal%3B%20font-weight%3A%20400%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20start%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20white-space%3A%20normal%3B%20widows%3A%202%3B%20word-spacing%3A%200px%3B%20-webkit-text-stroke-width%3A%200px%3B%20background-color%3A%20%23ffffff%3B%20text-decoration-style%3A%20initial%3B%20text-decoration-color%3A%20initial%3B%20display%3A%20inline%20!important%3B%20float%3A%20none%3B%22%3ESQL%20Server%202012%20(11.x)%20SP1%20CU2%3C%2FSPAN%3Eor%20higher%20version%20that%20supports%20BACKUP%20TO%20URL.%20If%20not%2C%20you%20would%20need%20to%20modify%20the%20script%20slightly%2C%20by%20taking%20a%20backup%20of%20the%20database%20to%20a%20local%20folder%20and%20then%20uploadomg%20the%20.bak%20file%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fstorage%2Fblobs%2Fstorage-quickstart-blobs-powershell%23upload-blobs-to-the-container%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20local%20folder%20to%20Azure%20Blob%20Storage%20container%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fdocs.dbatools.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDbaTools%3C%2FA%3Ecommand%20that%20backup%20your%20database%20to%20the%20temporary%20Azure%20Blob%20Storage%20account%20is%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EBackup-DbaDatabase%20-SqlInstance%20%24sourceInstance%20-Database%20%24sourceDatabase%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-AzureBaseUrl%20%22https%3A%2F%2F%24blobStorageAccount.blob.core.windows.net%2F%24containerName%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-BackupFileName%20%22WideWorldImporters.bak%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%60%20%20%20-Type%20Full%20-Checksum%20-CopyOnly%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20put%20a%20comma-separated%20list%20of%20databases%20that%20you%20want%20to%20migrate%20in%20the%20parameter%20%3CSTRONG%3E-Database%3C%2FSTRONG%3E.%20Once%20you%20complete%20this%20step%2C%20you%20will%20have%20the%20backups%20of%20all%20databases%20that%20are%20ready%20to%20be%20restored%20on%20Managed%20Instance.%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20taking%20a%20backup%20of%20a%20large%20database%20you%20might%20want%20to%20create%20a%20striped%20backup%20by%20using%20multiple%20URLs%20as%20target%2C%20and%20set%20of%20the%20following%20options%3A%20COMPRESSION%2C%20MAXTRANSFERSIZE%20%3D%204194304%2C%20and%20BLOCKSIZE%20%3D%2065536.%20Please%20refer%20to%26nbsp%3B%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20underline%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlcat%2F2017%2F03%2F10%2Fbacking-up-a-vldb-to-azure-blob-storage%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EBacking%20up%20a%20VLDB%20to%20Azure%20Blob%20Storage%3C%2FA%3E%26nbsp%3Bfor%20more%20information%20on%20how%20to%20use%20these%20options.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1943251395%22%20id%3D%22toc-hId-1943251395%22%3EMigrating%20the%20databases%20to%20the%20target%20instance%3C%2FH2%3E%0A%3CP%3ENow%20you%20need%20to%20setup%20the%20target%20Managed%20Instance%20where%20you%20need%20to%20create%20SAS%20token%20that%20will%20enable%20the%20Managed%20Instance%20to%20read%20.bak%20file%20from%20Azure%20Blob%20Storage%2C%20create%20a%20credential%20with%20this%20SAS%20token%2C%20and%20restore%20the%20database%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%23%20Generate%20new%20SAS%20token%20that%20will%20read%20.bak%20file%0A%24sas%20%3D%20(New-AzStorageAccountSASToken%20-Service%20Blob%20-ResourceType%20Object%20-Permission%20%22r%22%20-Context%20%24ctx).TrimStart('%3F')%20%23%20-ResourceType%20Container%2CObject%0A%20%0A%24targetLogin%20%3D%20Get-Credential%20-Message%20%22Login%20to%20target%20Managed%20Instance%20as%3A%22%0A%24target%20%3D%20Connect-DbaInstance%20-SqlInstance%20%24targetInstance%20-SqlCredential%20%24targetLogin%0A%0A%24targetCred%20%3D%20New-DbaCredential%20-SqlInstance%20%24target%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Name%20%22https%3A%2F%2F%24blobStorageAccount.blob.core.windows.net%2F%24containerName%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Identity%20%22SHARED%20ACCESS%20SIGNATURE%22%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-SecurePassword%20(ConvertTo-SecureString%20%24sas%20-AsPlainText%20-Force)%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Force%0ARestore-DbaDatabase%20-SqlInstance%20%24target%20-Database%20%24targetDatabase%20%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-Path%20%22https%3A%2F%2F%24blobStorageAccount.blob.core.windows.net%2F%24containerName%2FWideWorldImporters.bak%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3EThis%20script%20will%20prompt%20for%20the%20SQL%20login%20details%20that%20should%20be%20used%20to%20access%20Managed%20Instance.%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3EAs%20a%20result%2C%20you%20will%20get%20your%20database%20restored%20on%20the%20target%20Managed%20Instance%20and%20your%20database%20migration%20will%20be%20completed.%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--608905566%22%20id%3D%22toc-hId--608905566%22%3EServer-level%20objects%20migration%3C%2FH2%3E%0A%3CP%3EOnce%20you%20migrate%20your%20database%20objects%2C%20you%20might%20also%20need%20to%20migrate%20server-level%20objects%20such%20as%20Agent%20jobs%2Foperators%2C%20Database%20Mail%20configurations%2C%20server-level%20logins%2C%20etc.%3C%2FP%3E%0A%3CP%3EDbaTools%20provides%20a%20set%20of%20useful%20scripts%20that%20you%20can%20apply%20to%20migrate%20these%20objects%20using%20Copy-Dba%20commands%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ECopy-DbaSysDbUserObject%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaDbMail%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaAgentOperator%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaAgentJobCategory%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaAgentJob%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaAgentSchedule%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%0ACopy-DbaLogin%20-Source%20%24sourceInstance%20-Destination%20%24targetInstance%20-DestinationSqlCredential%20%24targetLogin%20-ExcludeSystemLogins%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20sample%20%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3Eselected%20object%20types%20from%20the%20source%20SQL%20Server%3C%2FSPAN%3Eare%20copied%20to%20target%20Managed%20Instance.%20You%20can%20change%20this%20part%20and%20select%20the%20objects%20that%20should%20be%20migrated.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1133904769%22%20id%3D%22toc-hId-1133904769%22%3ECleanup%3C%2FH2%3E%0A%3CP%3EIf%20your%20Azure%20Blob%20Storage%20account%20is%20temporary%20storage%20that%20you%20need%20to%20use%20only%20while%20you%20are%20moving%20databases%2C%20you%20can%20remove%20these%20resources%20using%20the%20following%20PowerShell%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ERemove-AzResourceGroup%20-Name%20%24resourceGroup%20-Force%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1418252192%22%20id%3D%22toc-hId--1418252192%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EAzure%20PowerShell%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.dbatools.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDBATools%20PowerShell%3C%2FA%3Elibraries%20enable%20you%20to%20easily%20script%20and%20automate%20and%20customize%20all%20parts%20of%20the%20migration%20process.%3C%2FP%3E%0A%3CP%3EThis%20type%20of%20migration%20is%20an%20offline%20migration%20where%20you%20probably%20need%20to%20stop%20any%20changes%20that%20might%20happen%20on%20the%20source%20instance%20until%20you%20finish%20the%20migration.%20As%20an%20alternative%2C%20you%20can%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdms%2Ftutorial-sql-server-managed-instance-online%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Migration%20Service%3C%2FA%3Eand%20perform%20an%20online%20migration.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-830801%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EIn%20Microsoft%20Azure%2C%20you%20can%20easily%20migrate%20your%20databases%20from%20SQL%20Server%20on-premises%20or%20Azure%20VMs%20to%20the%20fully-managed%20PaaS%20database%20service%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20underline%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fsql-database%2Fsql-database-managed-instance%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20SQL%20Database%20Managed%20Instance%3C%2FA%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E.%20In%20this%20post%20you%20will%20see%20how%20to%20create%20PowerShell%20scripts%20that%20automate%20migration%20to%20Managed%20Instance%20using%20Azure%20PowerShell%20and%20%3C%2FSPAN%3E%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20underline%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20href%3D%22https%3A%2F%2Fdocs.dbatools.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDbaTools%3C%2FA%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-830801%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EManaged%20Instance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

In Microsoft Azure, you can easily migrate your databases from SQL Server on-premises or Azure VMs to the fully-managed PaaS database service Azure SQL Database Managed Instance. In this post you will see how to create PowerShell scripts that automate migration to Managed Instance using Azure PowerShell and DbaTools.

The approach used in this article is OFFLINE migration. You should stop the activities on your source SQL Server instance while the databases and server-level objects are copied to the target Managed Instance. If you need ONLINE migration that will enable you to use your source databases while they are moving to Managed Instance, take a look at the Database Migration Service.

Typically, the steps involved in an offline migration process are:

  • Create an Azure Blob Storage account that will be used to temporary hold the database backups that will be moved from SQL Server to Managed Instance.
  • Back up the databases to Azure Blob Storage and restore them from Azure Blob Storage to Managed Instance.
  • Migrate server-level objects such as logins, agent jobs from the source to destination instance.

In this article, I will use Azure PowerShell to create and manage necessary Azure resources, and DBATools PowerShell library to initiate migration.

 

NOTE: This procedure is based on the assumption that the machine where you are running this script on has access both to the source SQL Server and the target Managed Instance.

 

Prerequisites

Before you begin, you should make sure that you have installed all necessary PowerShell libraries. The following PowerShell modules are used:

 

 

 

 

Install-Module Az.Resources
Install-Module Az.Storage
Install-Module dbatools

 

 

 

 

NOTE: Make sure that you have the latest 150.18147.0 or higher version of Microsoft.SqlServer.SqlManagementObjects installed or these commands will fail. If you don't install anything, DbaTools will include the latest version of SMO. However, if you have the older version and include it in the PowerShell session, the script will fail.

 

Parameter definition

As a first step, populate the parameters that define the names of source/target instances and temporary Azure Blob Storage account used in this sample:

 

 

 

 

# temporary resources needed for backups
$location = "westus"
$resourceGroup = "temp-migration-demo-rg"
$blobStorageAccount = "temp-migrationdemostorage"
$containerName = "backups"
 
# source and target instances
$sourceInstance = "SOURCESQLSERVER"
$sourceDatabase = "WideWorldImporters"
 
$targetInstance = "targetmi.public.920d05d7463d.database.windows.net,3342"
$targetDatabase = "WideWorldImporters"

 

 

 

 

Change these parameters depending on your needs. In this sample, I am using a public endpoint to connect to Managed Instance. If you don’t have a public endpoint, you can set up a Point-to-site connection or run the script from the virtual machine that is placed in the same subnet as your Managed Instance.

NOTE: Make sure that you use the same region where your Managed Instance is placed because this will speed-up the database restore process.

 

Storage setup

In order to move your databases, you need to create a temporary Azure Blob Storage account. If you already have one, you can skip this step.

The following PowerShell script creates an Azure Blob Storage account that will be used in this sample:

 

 

 

 

New-AzResourceGroup -Name $resourceGroup -Location $location
 
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
-Name $blobStorageAccount `
-Location $location `
-SkuName Standard_LRS `
-Kind StorageV2
 
$ctx = $storageAccount.Context
New-AzStorageContainer -Name $containerName -Context $ctx -Permission Container

 

 

 

 

Once you execute this script, you will have Azure Blob Storage account ready to be used in the migration.

 

Source instance setup

Once you create an Azure Blob Storage account, you need to generate a SAS key that will enable your SQL Server instance to access the Azure Blob Storage account and put the database backups there. This SAS key will be represented as a Credential object in SQL Server instance:

 

 

 

 

$sas = (New-AzStorageAccountSASToken -Service Blob -ResourceType Object -Permission "rw" -Context $ctx).TrimStart('?')
# Note: it must have r permission!
 
$sourceCred = New-DbaCredential -SqlInstance $sourceInstance `
                           -Name "https://$blobStorageAccount.blob.core.windows.net/$containerName" `
                           -Identity "SHARED ACCESS SIGNATURE" `
                           -SecurePassword (ConvertTo-SecureString $sas -AsPlainText -Force)

 

 

 

 

Now you are ready to back up your SQL Server databases to Azure Blob Storage.

 

Database migration

With the prepared Azure Blob Storage account and credentials, you can take the backups of you SQL Server databases and put them to Azure Blob Storage. I will assume that you have SQL Server 2012 (11.x) SP1 CU2 or higher version that supports BACKUP TO URL. If not, you would need to modify the script slightly, by taking a backup of the database to a local folder and then uploadomg the .bak file from the local folder to Azure Blob Storage container.

The DbaTools command that backup your database to the temporary Azure Blob Storage account is shown below:

 

 

 

 

Backup-DbaDatabase -SqlInstance $sourceInstance -Database $sourceDatabase `
                   -AzureBaseUrl "https://$blobStorageAccount.blob.core.windows.net/$containerName" `
                   -BackupFileName "WideWorldImporters.bak" `
               `   -Type Full -Checksum -CopyOnly

 

 

 

 

You can put a comma-separated list of databases that you want to migrate in the parameter -Database. Once you complete this step, you will have the backups of all databases that are ready to be restored on Managed Instance.

If you are taking a backup of a large database you might want to create a striped backup by using multiple URLs as target, and set of the following options: COMPRESSION, MAXTRANSFERSIZE = 4194304, and BLOCKSIZE = 65536. Please refer to Backing up a VLDB to Azure Blob Storage for more information on how to use these options.

 

Migrating the databases to the target instance

Now you need to setup the target Managed Instance where you need to create SAS token that will enable the Managed Instance to read .bak file from Azure Blob Storage, create a credential with this SAS token, and restore the database:

 

 

 

 

## Generate new SAS token that will read .bak file
$sas = (New-AzStorageAccountSASToken -Service Blob -ResourceType Object -Permission "r" -Context $ctx).TrimStart('?') # -ResourceType Container,Object
 
$targetLogin = Get-Credential -Message "Login to target Managed Instance as:"
$target = Connect-DbaInstance -SqlInstance $targetInstance -SqlCredential $targetLogin

$targetCred = New-DbaCredential -SqlInstance $target `
                           -Name "https://$blobStorageAccount.blob.core.windows.net/$containerName" `
                           -Identity "SHARED ACCESS SIGNATURE" `
                           -SecurePassword (ConvertTo-SecureString $sas -AsPlainText -Force) `
                           -Force
Restore-DbaDatabase -SqlInstance $target -Database $targetDatabase `
                   -Path "https://$blobStorageAccount.blob.core.windows.net/$containerName/WideWorldImporters.bak"

 

 

 

 

This script will prompt for the SQL login details that should be used to access Managed Instance.

As a result, you will get your database restored on the target Managed Instance and your database migration will be completed.

 

Server-level objects migration

Once you migrate your database objects, you might also need to migrate server-level objects such as Agent jobs/operators, Database Mail configurations, server-level logins, etc.

DbaTools provides a set of useful scripts that you can apply to migrate these objects using Copy-Dba commands:

 

 

 

 

Copy-DbaSysDbUserObject -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaDbMail -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaAgentOperator -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaAgentJobCategory -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaAgentJob -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaAgentSchedule -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin
Copy-DbaLogin -Source $sourceInstance -Destination $targetInstance -DestinationSqlCredential $targetLogin -ExcludeSystemLogins

 

 

 

 

In this sample selected object types from the source SQL Server are copied to target Managed Instance. You can change this part and select the objects that should be migrated.

 

Cleanup

If your Azure Blob Storage account is temporary storage that you need to use only while you are moving databases, you can remove these resources using the following PowerShell command:

 

 

 

 

Remove-AzResourceGroup -Name $resourceGroup -Force

 

 

 

 

 

Conclusion

Azure PowerShell and DBATools PowerShell libraries enable you to easily script and automate and customize all parts of the migration process.

This type of migration is an offline migration where you probably need to stop any changes that might happen on the source instance until you finish the migration. As an alternative, you can use Database Migration Service and perform an online migration.