Of late, I was working on a customer requirement, which is to start a runbook. Yes, many would have done it through logic apps, function app, or webhooks. This blog post is to walk you through the steps to create an Azure Function to start an Azure Automation runbook .
Our goal is to wait until the job completes and fetch the output. If you are familiar with C#, the ASYNC programming pattern helps you to achieve it quickly. In my case, it’s an IT Pro way. Yes, I am not good with ASYNC programming. So, I managed to convert my PowerShell script to C#, which solved the issue.
Why not an HTTP Trigger?
If the Azure automation account runbook job takes more than 230 seconds, a timeout occurs. The 230 seconds of timeout settings can’t be tuned, and it’s a hard set value. Azure Functions Start Azure Automation Runbook

Overview
What do we need?
- Azure Account.
- Automation Account.
- Runbook.
- SPN with Certificate.
- Azure Function App.
- VS code / Visual Studio (IDE is your choice)
Runbook
The runbook we need to start has three parameters as
illustrated below
- FirstName (String)
- SurName (String)
- Skills (Array of String)
For our demo purpose, the runbook should collect parameters and prints the data as JSON objects. Below, code does it.

Get Started
If you are familiar with PowerShell, the below snippet is
easy to understand.

The code above is to start an Azure automation account
runbook and retrieves the output.
Recode in native PowerShell
Do you think it’s reinventing the wheel? No, it is not. It’s
an alternative approach to avoid Az modules.
Using Azure .NET SDK in Azure Functions (Durable)
A simple “DO WHILE” loop is here to wait until the job gets
completed and retrieves it.
HTTP Start Trigger (Starting Point)

Orchestrator Function (A Gist)

Get Job Output

Client Application
The below snippet is to invoke the Azure function app.

Summary
We have covered the steps to use an Azure Function to start an Azure automation runbook, wait till the job completes and fetch the output. Indeed, there are multiple ways and approaches to do the same. If you have found one, please feel free to share your feedback. In my next blog post, we show the use case of a timer trigger with a real-world example.
Download Azure Migration Advantages guide