Achraf Ben Alaya
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy
SUBSCRIBE
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy
No Result
View All Result
Achraf Ben Alaya
No Result
View All Result
ADVERTISEMENT
Home Blog Cloud Azure

Azure Automation : How to create PowerShell Runbook

achraf by achraf
February 4, 2021
in Azure, Blog, Cloud
5 min read
0
Azure Automation :  How to create PowerShell Runbook
0
SHARES
1k
VIEWS
Share on FacebookShare on Twitter

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


Next Clique on Create .

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"

 

 

ShareTweet
Previous Post

Welcome to Azure Resource Mover service

Next Post

You forget to shutdown your virtual machine on your way home ? send an email and save your time

Related Posts

AI

Model Context Protocol (MCP): The Future of AI Integration

April 21, 2025
110
Azure

Step-by-Step Guide: Azure Front Door + Storage Account Static Website + Custom Domain with Terraform

March 11, 2025
232
Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet
Azure

Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet

February 3, 2025
137
Understanding Generative AI and RAG Benefits
AI

Understanding Generative AI and RAG Benefits

January 12, 2025
96
Azure Communication Services Email Sending Simplified: From Setup to Execution and Monitoring
Azure

Azure Communication Services Email Sending Simplified: From Setup to Execution and Monitoring

December 8, 2024
1.6k
PowerShell Automation for Azure Networks: Detailed VNET and Subnet Analysis
Azure

PowerShell Automation for Azure Networks: Detailed VNET and Subnet Analysis

November 2, 2024
502
Next Post
You forget to shutdown your virtual machine on your way home ? send an email and save your time

You forget to shutdown your virtual machine on your way home ? send an email and save your time

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Terraform

Certifications

Microsoft certified trainer (MCT)

Recommended

Block the default URL assigned to the azure web app

Block the default URL assigned to the azure web app

May 14, 2023
1.4k
Generating report for SSL Certificates for Websites with PowerShell

Generating report for SSL Certificates for Websites with PowerShell

April 10, 2022
1.1k
How to make the most of each day

How to make the most of each day

February 2, 2021
317
Reflecting on My Journey as a Microsoft MVP in 2024

Reflecting on My Journey as a Microsoft MVP in 2024

March 24, 2024
356
Navigating the Alphabet Soup: Unraveling Microsoft Acronyms

Navigating the Alphabet Soup: Unraveling Microsoft Acronyms

July 16, 2023
254
405 method not allowed in IIS

405 method not allowed in IIS

October 17, 2020
6.6k
Facebook Twitter LinkedIn Youtube

Model Context Protocol (MCP): The Future of AI Integration

April 21, 2025

Step-by-Step Guide: Azure Front Door + Storage Account Static Website + Custom Domain with Terraform

March 11, 2025
Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet

Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet

February 3, 2025

Categories

  • AI (2)
  • Apps (1)
  • Azure (63)
  • blazor (2)
  • Blog (91)
  • c# (7)
  • Cloud (65)
  • Courses (3)
  • Dapr (4)
  • docker (4)
  • Games (1)
  • General Tips & Fix (1)
  • Home (1)
  • Kubernetes Service (AKS) (1)
  • motivation (2)
  • Motivation (3)
  • News (9)
  • Resume (1)
  • sql (4)
  • Terrafrom (1)
  • Tricks, Tips and Fixes (4)
  • xamarin (5)
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy

ADVERTISEMENT