Azure Automation PowerShell Script

Azure Automation – Twitter + IFTTT + Webhook = Start Runbook

image

I assume you know Twitter and you probably also know what a webhook is, right? No? Ok, a webhook is just a HTTP POST. In Azure Automation we are able to create a webhook for a runbook. This runbook will “consume” the webhook request  (URL) + post data and start the runbook. The cool thing is, that you are able to trigger a runbook in a secure way without the need of credentials and you are able to pass parameters within this request. Well, this is nothing special in todays world, but sometimes the combination of things make the magic.

Another technology, which has been around for a few years is IFTTT (If This Than That), this is a online service that let’s you choose a channel A (trigger) and if a certain condition happens it will trigger channel B (action). For example channel A could check the weather in Switzerland (because you are planning a trip to Switzerland) and if it will start raining you could trigger channel B to receive a warning by email. This combination of channels is called a “recipe”. You can choose from dozens of channels and combine them as you like. I highly recommend to check this service out, it is easy and fun.

In this post I want to show how to trigger a webhook / runbook, if someone is tweeting about SCOM.

1) Create a runbook

In Azure Automation I just created a simple PowerShell runbook that looks like this called Hello-Twitter

image

param (
 [object]$WebhookData
 )

if ($WebhookData -ne $null) 
	{
		$BodyContent = $WebhookData.RequestBody
		Write-Output "There was a tweet from $BodyContent"
	}
else
	{
		Write-Error "Something went wrong buddy"	
	}

There is not much more to say than that, so let’s create a webhook next.

2) Create a webhook

So there are two ways you could create a webhook either by GUI or using PowerShell. I prefer to use PowerShell, because it is easier. Make sure you change the parameter to your environment accordingly. In this example the webhook will expire in 10 days from now…

$Credential = Get-Credential

#Authenticate to Azure and AzureRM
Add-AzureAccount -Credential $Credential | Out-Null
Add-AzureRmAccount -Credential $Credential | Out-Null

#Provide the necessary information for your environment
$Webhook = New-AzureRmAutomationWebhook `
 -Name "TriggeredByTwitter"`
 -RunbookName "Hello-Twitter"`
 -IsEnabled 1 `
 -ExpiryTime (Get-Date).AddDays(10)`
 -ResourceGroupName "Automation"`
 -AutomationAccountName "AutomationAccount"

#Print the webhook uri
Write-Host $Webhook.WebhookUri -ForegroundColor Green

If you don’t have Azure PowerShell installed locally and you want to know how to do it read this blog here. If you have done so, you are able to execute this script in ISE or your favorite PowerShell editor.

After you executed the script, you will see something like this…

image

Make sure you copy the URI (green output) to Notepad because we will need it later on.

If I check in the GUI, everything seems ok…

image

So far we have the runbook and the webhook, next we will create the IFTTT recipe.

3)Create IFTTT recipe

First go to http://ifttt.com and create an account. Then click Create Recipe

image

Choose Twitter channel…

image

Choose New tweet from search…

image

Then type hashtag #SCOM and hit Create Trigger

image

So that is the IF part or the trigger next we do the actual action for our webhook…

Click on the that link and you will be redirected to choose the action channel. Search for Maker….

image

Choose make a web request….

image

In this step comes the real meat. Paste the webhook URI into the URL field, set the Method to POST and the Content Type to application/json. Into the body of the request we add the UserName field of the user who tweeted about SCOM. This data comes from the previously configured Twitter channel. Then hit Create Action.

image

And create the recipe…

image

So that’s it :). If you configured everything properly you will see in the Azure portal that the runbook gets triggered and the output looks like this…

image

Everytime someone tweets about SCOM my runbook get’s triggered. I think that rocks!

There are endless possibilities to combine these technologies and build cool solutions. Here I just wanted to show, how to get started and deliver a simple idea.

3 Replies to “Azure Automation – Twitter + IFTTT + Webhook = Start Runbook

  1. Hello, I am trying to replicate this example, but with the intention, always get the last tweet of a specific user.

    Take your runbook equally, and I did webhook by azure GUI because no tngo knowledge about the use of the platform, and I just want to get what I stated in the paragraph above.

    I wonder if you could help me solve that problem, thanks.

Leave a Reply to proyectoupc (@proyectoupc2016) Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.