How to Run PowerShell Code by Invoking a Webhook in Azure Automation

Back in my day, we could only run PowerShell scripts on local Windows computers. Nowadays though. it’s amazing all of the different ways to execute PowerShell scripts. We not only have PowerShell on Linux but Azure Cloud Shell too! Azure Cloud Shell is an awesome way to quickly bring up a PowerShell environment to execute code in the cloud. Speaking of Azure, we’ve also got Azure Automation runbooks. Using PowerShell Azure Automation runbooks are another great way to get stuff done with PowerShell.

Whenever I build a PowerShell Azure Automation runbook, the trigger for the runbook has always been on a schedule or invoked via another runbook. However recently, I needed the ability to invoke it outside of Azure completely via a service we use here at TechSnips called Zapier.

Are you an IT pro ready to take your career to the next level? If so, join our contributor program! You don't have to be an expert presenter, a Microsoft MVP or even have a blog. We want your knowledge! You will be coached on presentation skills, become a member of our awesome community and get paid some nice side hustle income every, single month! Sign up today!.

Zapier allows me to link together a lot of automation workflows but unfortunately, it has no native support for PowerShell. It has a Run Code action but’s it’s just Python. It does, however, have a Webhook action which allows me to send HTTP GET, POST, PUT and other verbs to a web service. It turns out that Azure Automation allows me to attach webhooks to my runbooks to be triggered outside of Azure Automation!

I needed a way to invoke any kind of PowerShell code via webhook so I’ve created a runbook that allows me to do just that. The script is below and can be found on the SnipScripts GitHub repository.

This script allows you to pass a PowerShell scriptblock to a runbook with parameters and run it. It uses the built-in $WebhookData parameter in Azure Automation to bring in data sent via the HTTP request body. For more information on this object, refer to the article Starting an Azure Automation Runbook with a Webhook.

Create the PowerShell Script

Create the Webook in Azure Automation

Once you’ve got the script created, you’ll need to create a runbook. I’ve called mine RunPowerShellScript and was sure the runbook type was PowerShell.

Once the runbook was created, you’ll then click Edit and paste in the code you’ve got above.

Once the runbook is created, it’s then time to create the webhook. To do that, you’ll click on Webhooks –> Add Webhook.

Create the Webhook

Be sure to copy the webhook URI otherwise you’ll have to create another one!

You’re not going to be specifying any default values for your parameters so leave the parameter section blank.

Setup the HTTP POST Request

Once you’ve got the runbook and webhook set up, you’ll then need to craft your request. I’ve built the script in a way that allows you to create a hashtable with an Expression and Parameters key. The Expression key is a scriptblock containing the code you’d like to execute include any references to parameters. Notice below that I’m demonstrating a parameter called namedParam1.

If you’ve defined a parameter reference inside of the expression, you’ll need to also use the Parameters key in the hashtable. This allows you to create another hashtable with all of the parameter names and values that will be passed to the expression at run-time.

Send the request to the webhook URI

Once you’ve created the request hashtable, you can then paste in your webhook URI and then issue your HTTP POST request to the webhook but remember to convert your request object to JSON first.

Verify the output of the job

Finally, check the output from the job in your runbook and notice that the runbook has ran and it’s successfully passed in the parameter.

Leave a Reply

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