Introduction
Azure Automation delivers a cloud-based automation and configuration service that supports consistent management across your Azure and non-Azure environments. It comprises process automation, configuration management, update management, shared capabilities, and heterogeneous features. Automation gives you complete control during deployment, operations, and decommissioning of workloads and resources.
Azure Automation give you access to :
-
Process Automation (Orchestrate processes using graphical ,PowerShell and python runbooks).
-
Configuration Management .
- Change Tracking and Inventory.
- Shared capabilities (shared resources, role-based access control, flexible scheduling… ) .
- heterogeneous (Windows & Linux azure and on premises) .
Steps :
Create an Azure Automation Account .
Before we create our first Run-book ,first we are going to create an automation account by following the below steps
Explore Runbooks.
Now after we created the Automation account ,we need to go to Runbooks ,there we will find some examples of each type of runbooks that you can create .
Now we are going to create our first runbook .
Create a Runbook
To create a runbook ,first we need to clique on “Create a runbook” as in picture below
We need to fill the name of the runbook the runbook type and you can write a description too .
For our demo we are going to use PowerShell :
After Azure creates the runbook, it should redirect you to the Edit PowerShell Runbook page. This page is where you can enter in the PowerShell code that the Automation Account executes.
Let’s write our first runbooks by adding a text to our editor
“Hello, Welcome to Azure Runbooks!”
Once you have entered the code, select the Save button, then the Publish button.
Execute the Runbook
After the runbook have been created and published now we can execute it .
Select Start button and you can see the status of the runbook now .
As you can see up , there is different tabs for : Input, output , Errors , Warnings ,All logs and Exception if ever happened .
Let’s create an Error:
we need to go back to our PowerShell and add :
“Write-Error -Message “This is the error message sample.”
Now our code will look like :
"Hello, Welcome to Azure Runbooks!" Write-Error -Message "This is the error message sample."
Save and Publish again .
Adding parameters
Parameters allow PowerShell scripts to be more dynamic instead of setting static variables.
param ( [Parameter()] [string] $FirstName = "Achraf" ) "Hello $FirstName " "Hello, Welcome to Azure Runbooks!" Write-Error -Message "This is the error message sample."
Global Variable Assets :
You can also define variables within the Automation Account to be used by multiple runbooks .
param ( [Parameter()] [string] $FirstName = "Achraf" ) $congratsMessage = Get-AutomationVariable -Name 'congratulation' "Hello $FirstName , $congratsMessage"
Exploring the gallery (Manage modules in Azure Automation)
The automation account contain a gallery that contain a lot of samples that can be very useful .
@{'text'='oink'}, @{'text'='meo'} | Write-ObjectToSQL -Server localhost\sqlexpress -Database MyDB -TableName myTable
This will generate a table with a column called ‘text’ which will contain the values ‘oink’ and ‘meo
Get-Process | Write-ObjectToSQL -Server localhost\sqlexpress -Database MyDB -TableName ProcessTable
This will Create a table called ProcessTable (if it doesnt exist) based on the result from Get-Process. After the table is created all the objects will be inserted into the table.
My sample SQL RunBook
Here I have a sample that I have created a year ago that will call a stored procedure using the Schedule Runbook to run everyday at a specified time .
Write-Output "Run started" # Instantiate the connection to the SQL Database $sqlConnection = new-object System.Data.SqlClient.SqlConnection $sqlConnection.ConnectionString = "" $sqlConnection.Open() Write-Output "Azure SQL database connection opened" # Define the SQL command to run $sqlCommand = new-object System.Data.SqlClient.SqlCommand $sqlCommand.CommandTimeout = 120 $sqlCommand.Connection = $sqlConnection Write-Output "Issuing command to run stored procedure" # Execute the SQL command $sqlCommand.CommandText= 'exec [dbo].[Calculate_rate]' $result = $sqlCommand.ExecuteNonQuery() Write-Output "Stored procedure Calculate_rate execution completed" # Close the SQL connection $sqlConnection.Close() Write-Output "Run completed"