I have not actually tried this (but it peaks my interest to later).
You will not be able to use Restore-Database
because it only reads native backup formats from SQL Server, not Azure SQL.
The BACPAC
file created from your backup of an Azure SQL database is in a data-tier format. Which in this format contains the schema and data for the database. In order to restore this to a local instance in PowerShell you will have to utilize the DAC
objects to do the restore
. Which if you are loading the SQLPS
module the DAC
assembly is loaded for you.
Basic examples that I find online seem to follow this format:
Add-Type -path "C:\Program Files (x86)\Microsoft SQL Server\<version>\DAC\bin\Microsoft.SqlServer.Dac.dll"
$restoredDatabaseName = 'NewDatabase'
$bacpacFile = "Your BACPAC file you downloaded"
$conn = "Data Source=Server;Initial Catalog=master;Connection Timeout=0;Integrated Security=True;"
$importBac = New-Object Microsoft.SqlServer.Dac.DacServices $conn
$loadBac = [Microsoft.SqlServer.Dac.BacPackage]::Load($bacpacFile)
$importBac.ImportBacpac($loadBac, $restoredDatabaseName)
#Clean up
$loadBac.Dispose()
Once I get something setup I will test the above myself, just to make sure.
Update
The dbatools PowerShell module now includes commands around managing DacPac files. This can be utilized to perform export and publish operations.
Publish-DbaDacpac. I have not had chance to test this against an Azure SQL instance.
The error indicates that your connection string is wrong.
Verify that your string concatenates correctly to what you're expecting.(I'm also not familiar with your use of credential, so that may cause issues if it doesn't expose the password in the way you're expecting. You may want to consider the constructor that takes a credential object as an argument)
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer,$using:SqlServerPort;Database=$using:Database;User ID=$using:SqlUsername;Password=$using:SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
Looks like it might need to be
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$($SqlServer),$($SqlServerPort);Database=$($Database);User ID=$($SqlUsername);Password=$($SqlPass);Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
Best Answer
I tried to work on this question...I hope it helps who is looking for it.The solution script is as follows:
Set subscription
Create a new resource group
Create a new server with a system wide unique server name
Create a server firewall rule that allows access from the specified IP range
Create an elastic database pool:
Declare an array to store db names:
Loop to create automatic multiple database as per requirement: