Azure Automation Runbook Webhook lesson learned


You can start an Azure Automation Runbook with a webhook. A webhook allows you to start a particular runbook in Azure Automation through a single HTTP request. This allows external services such as Visual Studio Team Services, GitHub, Microsoft Operations Management Suite Log Analytics, or custom applications to start runbooks without implementing a full solution using the Azure Automation API.

Lesson learned:

Your Runbook parameter needs to be called Webhookdata.

Suppose you want to create an Azure Automation Runbook called Hello World which outputs your first and last name.

Example Hello World PowerShell script runbook:

[CmdletBinding()]
param(
    $firstname,
    $lastname
)

Write-Output "Hello $firstname $lastname"

If we run this Runbook and enter the following input for the firstname and lastname parameters:

  • Stefan
  • Stranger

We get the following returned:

image

If we want to call this Runbook from a Webhook we first need to configure the webhook for this Runbook.

image

Create and configure the webhook.

image

Don’t forget to copy and store the URL!

image

Click create to create the webhook.

 

If we now try to use the following code to trigger the webhook the Runbook does not work as expected.

$webhookurl = 'https://s2events.azure-automation.net/webhooks?token=[secrettoken]'

$body = @{"LastName" = "Stranger"; "FirstName" = "Stefan"}

$params = @{
    ContentType = 'application/json'
    Headers = @{'from' = 'Stefan Stranger'; 'Date' = "$(Get-Date)"}
    Body = ($body | convertto-json)
    Method = 'Post'
    URI = $webhookurl
}

Invoke-RestMethod @params -Verbose

The Output windows shows the following:

image

No FirstName and LastName is being shown.

If we look at the Input for this runbook we see that the input parameter from the webhook is called WEBHOOKDATA.

image

If we change the Runbook code to the following it works ok:

[CmdletBinding()]
Param
([object]$WebhookData) #this parameter name needs to be called WebHookData otherwise the webhook does not work as expected.
$VerbosePreference = 'continue'

#region Verify if Runbook is started from Webhook.

# If runbook was called from Webhook, WebhookData will not be null.
if ($WebHookData){

    # Collect properties of WebhookData
    $WebhookName     =     $WebHookData.WebhookName
    $WebhookHeaders  =     $WebHookData.RequestHeader
    $WebhookBody     =     $WebHookData.RequestBody

    # Collect individual headers. Input converted from JSON.
    $From = $WebhookHeaders.From
    $Input = (ConvertFrom-Json -InputObject $WebhookBody)
    Write-Verbose "WebhookBody: $Input"
    Write-Output -InputObject ('Runbook started from webhook {0} by {1}.' -f $WebhookName, $From)
}
else
{
   Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}
#endregion

#region Main
$FirstName = $Input.FirstName
$LastName = $Input.LastName

 Write-Output -InputObject ('Hello {0} {1}.' -f $FirstName, $LastName)
 #endregion

 

Result:

image

 

Hope this clarifies how to use a Webhook for an Azure Automation Runbook.

 

References:


Comments (1)

  1. Anton Masyan says:

    omg, thanks! 🙂 I broke my mind when I was trying to understand why my param from JSON didn’t work.

Skip to main content