Tuesday, 28 November 2017

Schedule SQL Jobs Using Azure Automation account

With the migration to SQL Azure happening on a fast pace i get lot of queries from my clients on how to schedule a SQL Agent job on a SQL Azure database In this Blog We will see how to schedule a SQL how to schedule a SQL Job using Azure Automation Account.

Environment:-

I am going to use my Test server 'Vinothtestsqlserver' and Test database 'VinothSampleDatabase' for this example.



Login into to Azure Subscription of the SQL Server and create new Automation Account from Azure MarketPlace



Once the Automation Account is created, Scroll through the left tab and select Runbooks under Process Automation,


First Step is to create a Credential for the SQL connection, Similar to Credential in SQL Agent or Data Sources in SSIS,  Under Shared Resources on you Automation Account Page, Select Credentials and Create a new Credential as below.


I have created a Credential Named BlogTestCredential for this example,



Now Create New Run book-This is similar to creating a new SQL agent Job


I have named the Runbook 'VinothTestRunBook' and Will be using Powershell Workflow. SQL Jobs can be created with either Powershell or Powershell Workflow.


Once the Runbook is created Your Script Editor will open, My aim for the SQL Job is to run 'SELECT * FROM sys.tables' from the VinothSampleDatabase every Hour.

I have used the below Powershell Script to achieve the output, The Script take Servername and Database as Parameter, then Uses BlogTestCredential using Get-Credential cmdlet to connect the SQL database. It then uses SQLConnection and SQLCommand Class to run the SQL.

Workflow VinothTestRunBook
{

param
    (
        # Fully-qualified name of the Azure DB server 
        [parameter(Mandatory=$true)] 
        [string] $SqlServerName,
        
        [parameter(Mandatory=$true)] 
        [string] $DataBaseName

        # Credentials for $SqlServerName stored as an Azure Automation credential asset
        # When using in the Azure Automation UI, please enter the name of the credential asset for the "Credential" parameter
        #[parameter(Mandatory=$true)] 
        #[PSCredential] $Credential
    )

     inlinescript
    {
        $Credential = Get-AutomationPSCredential -Name 'BlogTestCredential'
        # Setup credentials   
        $ServerName = $Using:SqlServerName
        $DBName = $using:DataBaseName
        $UserId = $Credential.UserName
        $Password = ($Credential).GetNetworkCredential().Password

        # Create connection to Master DB
        $MasterDatabaseConnection = New-Object System.Data.SqlClient.SqlConnection
        $MasterDatabaseConnection.ConnectionString = "Server = $ServerName; Database = $DBName; User ID = $UserId; Password = $Password;"
        $MasterDatabaseConnection.Open();

        # Create command to query the current size of active databases in $ServerName
        $MasterDatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
        $MasterDatabaseCommand.Connection = $MasterDatabaseConnection
        $MasterDatabaseCommand.CommandText = 
        "SELECT * from sys.tables"
                
        # Execute reader and return tuples of results 
        #$MasterDbResult = $MasterDatabaseCommand.ExecuteReader()
        #if ($MasterDbResult.HasRows)
        #{
              #$MasterDbResult.Read()|SELECT *
        #}
        # # Execute the query
        #$DatabaseCommand.ExecuteNonQuery() #ToExec SP
        $Ds=New-Object system.Data.DataSet
        $Da=New-Object system.Data.SqlClient.SqlDataAdapter($MasterDatabaseCommand)
        [void]$Da.fill($Ds)

        # Output the count
        #$Ds.Tables.Column1
        $Ds.Tables[0]|ft -a

        $MasterDatabaseConnection.Close
    }

}

In the Above Example I have used SQL Adapter to get the output and display, But in case you want to run a Store Proc or run Updates or Deletes use the Appropriate method(eg. ExecuteReader(), ExecuteNonQuery(), ExecuteScalar() etc.. More details on SQLCommand class and method in the below MSDN Link.

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand%28v=vs.110%29.aspx


Once you Finish Scripting Test the Script by Clicking the Test Pane Button, where you can test the script by specifying the parameters(Server name and DB Name in this case) as below.


Once your testing Successful, Publish your run book by clicking the publish button this is same as creating a SQL Agent Job without a Schedule. 


Now once your Runbook is publish we can create a Schedule, Go back to Runbook Page on the Portal and select Schedules and create a new schedule as below I have created similar to SQL Agent job to run this script every hour,



Once you Create the Schedule Specify the Parameters for the Run book Schedule as below,


That's about it your job is now ready to run, To check the Job history Go to Workbook page in the Portal and click Jobs


Now you can see summary of a completed job, for detailed information click on the summary


And You can see the Jod Run's detailed info, To see the output click the output pane and you can see the job output.



There you go now you have perfectly working SQL Agent Environment in Azure. Powershell Workflow Allows you you run Steps in Parallel and in sequence we will see in detail on future blogs.





No comments:

Post a Comment