Can I use Azure CLI to Create and populate an Azure SQL Server Database using PowerShell, What I need to do that?

Hi,
I couldn’t find any resource that talk about connecting to Azure SQL Server Instances and creating tables or inserting data in tables in a database created for trend analysis , Is there any way that I can do that using Azure PowerShell , Azure CLI or Azure Bicep ?

I only found that I can use SQLCMD , and still with it I have difficulties related to a newly created Service Principal?

Your help is highly appreciated.
Thanks

Introducing the Azure Az PowerShell module | Microsoft Learn looks like they have a sub-module az.sql. have you looked into that?

Yes, I know about it but it is just for Azure processing and services, there is no things like "CREATE TABLE or “INSERT INTO TABLE” , Things I need to do to collect specific data from multiple databases

Apologies, I don’t know what you have tried or what info you know :slight_smile: I recalled seeing a SQL sub-module and figured it might be something to look into. Please understand you asked a very general question. People trying to help have varying levels of experience and aren’t going to have all the answers. Some of us may not even work in a space you ask about, but we do our best to try and help. The more information, context, data you can provide us, the better.

Query Azure SQL Database using Service Principal with PowerShell – Thomas Thornton – Microsoft Azure MVP – HashiCorp Ambassador might have what you’re looking for, since it talks specifically about service principals. In this case however, he’s creating a SQL connection using more of a c# method. It looks like MS has some docs on this as well here:

Connect to Azure SQL with Microsoft Entra authentication and SqlClient - ADO.NET Provider for SQL Server | Microsoft Learn. There’s a section on using service principal auth, which is what I assume you’re doing.

It seems like with the first article, you get a token for use to build into your connection string, and they are using AZ PS module for that, which is something I’ve seen done in the past. I’m not aware of a module that would have the concept of azure service principals and be able to pass it information (like in graph) to authenticate to a DB to do things, so using AZ to generate an auth token to use that in a connection string I think. That seems to somewhat align with the MS learn article shared.

That said, I do recall seeing the PowerShell SQL Module a connectionstring parameter. If I were you, I might try to give that a shot. I don’t know if it would work. That would involve generating the token based off the first article then seeing if you’re able to use the connection string you build in one of those modules. If not, it seems like the C# sqlclient object way is documented/supported, and you should be able to run that in PS. I don’t have a way to test sorry. Several articles share how they do it with ‘normal’ accounts by getting the connection info and just using Invoke-SQLCMD so that’s also something.