How to run stored procedure using Azure Automation? How to run stored procedure using Azure Automation? powershell powershell

How to run stored procedure using Azure Automation?


I found the core of the problem, the code works just fine, the issue was I was using the wrong type of RunBook inside Azure Automation, so, make sure you're running a Workflow PowerShell instead of a simple PowerShell.

The code I posted in the question works, but I found a better way to understand what the code made by using the example provided here: https://azure.microsoft.com/en-us/blog/azure-automation-your-sql-agent-in-the-cloud/ (thanks to @Joseph Idziorek)

Here is the working code for anyone who ran into the same problem as i did:

workflow NAME-OF-YOUR-WORKFLOW{    Write-Output "JOB START BEFORE INLINESCRIPT"    inlinescript    {        Write-Output "JOB START"        # Create connection to Master DB        $MasterDatabaseConnection = New-Object System.Data.SqlClient.SqlConnection        $MasterDatabaseConnection.ConnectionString = "Data Source=YOUR-DATABASE-SERVER-NAME.database.windows.net;Initial Catalog=YOUR-DATABASE-NAME;Integrated Security=False;User ID=YOUR-DATABASE-USERNAME;Password=YOUR-DATABASE-PASSWORD;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"        $MasterDatabaseConnection.Open()        Write-Output "CONNECTION OPEN"        # Create command        $MasterDatabaseCommand = New-Object System.Data.SqlClient.SqlCommand        $MasterDatabaseCommand.Connection = $MasterDatabaseConnection        $MasterDatabaseCommand.CommandText = "YOUR-PROCEDURE-NAME"        Write-Output "DATABASE COMMAND TEXT ASSIGNED"        # Execute the query        $MasterDatabaseCommand.ExecuteNonQuery()        Write-Output "EXECUTING QUERY"        # Close connection to Master DB        $MasterDatabaseConnection.Close()         Write-Output "CONNECTION CLOSED"    }        Write-Output "WORK END - AFTER INLINESCRIPT"}

The Write-outputs are optional, if you want to check what part of the code is working and if everything worked after each run.


The following article and subsequent sample code should be a good starting place to get PowerShell code executing against Azure SQL Database from Azure Automation: https://azure.microsoft.com/en-us/blog/azure-automation-your-sql-agent-in-the-cloud/