How do you run many SQL commands against an Azure SQL database using an Azure Automation powershell runbook - powershell

I'm using Azure Automation to move an Azure SQL database from one resource to another(from Prod to Dev for example). After the database is copied, I would then like to run SQL script that adds some users and permissions. This would mean I need to run a handful of commands like "Create user..." and "alter role....". Most examples I've found use powershell to execute a single SQL command, but using that code to run many commands seems like it would result in an excessively long powershell script. In the on-prem world, I probably would have .sql file that gets executed. Any suggestions on how to achieve this easily using powershell in Azure Automation. Thanks!

Related

Read .sql files with Azure Automation runbook

I am trying to connect my powershell runbook to a storage account (blob) to read .sql files and execute them in my Azure SQL Database.
Connect to a blob container
Read the script on .sql file
Execute the script on the db
When I try Invoke-Sqlcmd, it requires a dedicated storage to store the file. However, runbooks work serverless and there is no storage I can use for the files, as far as I know.
Is there a way to only read the files (without moving them around) via powershell runbook or can I store the files to read with it?

Running a script from gitlab in Jenkins

I have an sql script in Github that I would like to run in my Jenkins build. I connected to GitHub in SCM through ssh keys and have connected to the database in the build step (using execute shell). Now, I would like to run that sql script on the database I connected to. I'm not sure how to call it from GitHub and select the specific version I would like to call.
Assuming you have sqlplus setup in the Jenkins server. First, check out the script from Github and then you can directly pass the .sql script to sqlplus. See the below example. You can simply use the # and the script location.
sqlplus username/password#orcl #your_script.sql

How to backup and restore databases through stored procedure in Azure SQL using Elastic Pool?

I have a C# ASP.NET MVC based web application in which there is a functionality of Archiving (backup) and Restoring Databases on the click of button. Currently, our system is On-Prem and so we are using the stored procedures in SQL Server to do the backup and restore.
Now we are migrating to Azure PaaS and are using Elastic Pool. As the backup and restore T-SQL commands do not work in Azure SQL, so can someone please help me in finding a way to do the backup and restore of DBs manually through T-SQL?
Any help would be highly appreciated!
Auto backups is a feature of Azure SQL database.
In SQL Azure, database backups are executed automatically and it is not possible to change this behavior. This is a service offered when you create a SQL Azure database and the first full backup occurs immediately after you create a new SQL Azure database and the rest of the backups are scheduled by SQL Azure itself.
There is no way can help you backup and restore of DBs manually through T-SQL.
Hope this helps.
Thanks for the quick answer,Leon! :)
So that it could be helpful to other community members, I would like to mention another way to achieve the same as it can't be done by T-SQL.
What I have done instead is, I have used SQLPackage.exe utility embedded in a batch script and executed that batch script through my C# code to achieve the same functionality.

How to use PowerShell to refresh a dev database from backup of prod

We are using DDBoost to backup and restore SQL Server databases.
Now we want to create a script or a job, so developers can kick off the script or the job to refresh their dev databases without asking a DBA.
I know in SQL Server Management Studio we can't take input in Job so I want to create a script with T-SQL or CLI or Powershell to take inputs like SourceDB, TargetDB, and then refresh a dev database using with the parameters.
I know how to take input in PowerShell so if someone can tell how to do it either by:
Using powershell to restore from DDBoost
Passing the values from PowerShell to T-SQL or CLI
Any other option in SQL Server management Studio.
I'm going to ignore the fact about DDBoost, because I don't know it.
But I know about dbatools.io, which is very solid powershell module that does exactly what you are looking for. It can with one line of powershell restore an entire database. You can make it as simple or advanced as you want.
Prerequisites:
A working SQL Server backup file. This can be created from SQL
Server Management Studio or dbatools.io
A SQL Server
The executing user needs privileges to restore the database.
Install dbatools
Installation
Start PowerShell (Run As Administrator)
Install-Module dbatools
Backup database
Here I'll show how you can backup a database using dbatools.
Backup-DbaDatabase -SqlInstance servername -Database databasename -BackupDirectory C:\Temp -CompressBackup
Restore database
Here I'll show how you can restore a database using dbatools.
Restore-DbaDatabase -SqlInstance servername -DatabaseName databasename -Path C:\Temp\filename.bak -WithReplace -useDestinationDefaultDirectories -ReplaceDbNameInFile
The restore script instructs the SQL Server to overwrite any existing database. It will restore the database files into the folders that are configured inside the SQL Server configuration and it will replace the database name in the physical files.

Running shp2pgsql in Azure cloud shell

I'm working with an Azure Postgresql database and am using the Cloud Shell to run psql scripts without problems. I'm now trying to load some shp files via the shp2pgsql command. The cloud shell responds by:
bash: shp2pgsql: command not found
Is it possible at all to use shp2pgsql with the Cloud Shell or I'm missing something? I've already successfully created the postgis extension on the Postgresql server.
Unfortunately, it seems that you cannot run the shp2pgsql command in the Azure Cloud Shell. It is just an interactive, browser-accessible shell for managing Azure resources. Not integrated with too much tool in it because of its flexibility. You can get more details about the features from the Features & tools for Azure Cloud Shell.
I suggest if you want to do something complicated, you'd better run it in a specific Azure VM for yourself. Hope this will be helpful to you.