$ErrorActionPreference = "stop" Import-Module AzureRM # This pops open a GUI for login. # Making the login process silent and unattended is out of scope for this article. Login-AzureRmAccount # Export bacpac file to local directory $DACversion = "140" $DACpath = "C:\Program Files (x86)\Microsoft SQL Server\$DACversion\DAC\bin" $ServerInstance = "SQLHAMMERLAPTOP\SQL2017CTP20" $Database = "AdventureWorks2014" $OutputFileName = "AdventureWorks2014.bacpac" $OutputDir = "C:\Backups\" $OutputPath = Join-Path $OutputDir $OutputFileName Set-Location $DACpath .\sqlpackage.exe /Action:Export /SourceDatabaseName:$Database /SourceServerName:$ServerInstance /TargetFile:$OutputPath /OverwriteFiles:True # Copy bacpac file to Azure storage account # Download and install Microsoft Azure Storage Tools # http://aka.ms/downloadazcopy $AzCopyDir = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy" $resourcegroupname = "LaunchDB-bacpac-RG" $StorageAccount = "bacpacrepository" $ContainerName = "bacpacs" $StorageURI = "https://$StorageAccount.blob.core.windows.net/$ContainerName/" $StorageKey = $(Get-AzureRmStorageAccountKey -ResourceGroupName $resourcegroupname -StorageAccountName $StorageAccount).Value[0] Set-Location $AzCopyDir .\AzCopy.exe /Source:$OutputDir /Dest:$StorageURI /DestKey:$StorageKey /Pattern:$OutputFileName # Import bacpac into an Azure SQL Database # Requires the AzureRM module # https://docs.microsoft.com/en-us/powershell/azure/install-azurerm-ps?view=azurermps-4.1.0 $location = "East US" $adminlogin = "SQLHammer" $password = "MySuperSecretPassword" $servername = "launchdb-bacpac" # Import bacpac to database with an S3 performance level $importRequest = New-AzureRmSqlDatabaseImport -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -DatabaseName $Database ` -DatabaseMaxSizeBytes "262144000" ` -StorageKeyType "StorageAccessKey" ` -StorageKey $(Get-AzureRmStorageAccountKey -ResourceGroupName $resourcegroupname -StorageAccountName $StorageAccount).Value[0] ` -StorageUri "$StorageURI$OutputFileName" ` -Edition "Standard" ` -ServiceObjectiveName "S3" ` -AdministratorLogin $adminlogin ` -AdministratorLoginPassword $(ConvertTo-SecureString -String $password -AsPlainText -Force) # Check import status and wait for the import to complete $importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink Write-Host "Importing" -NoNewline while ($importStatus.Status -eq "InProgress") { $importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink Write-Host "." -NoNewline Start-Sleep -s 10 } Write-Host "" $importStatus # Scale down to Basic after import is complete Set-AzureRmSqlDatabase -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -DatabaseName $Database ` -Edition "Basic" ` -RequestedServiceObjectiveName "Basic"