How to Start a PowerShell Runbook by Webhook in Azure Automation Using Power Automate

Introduction

 
Azure Automation provides a cloud-based automation and configuration service that provides consistent management across your Azure and non-Azure environments. Azure Automation helps to automate frequent, time-consuming, and error-prone cloud management tasks. It consists of process automation, update management, and configuration features. 
 
Azure Automation helps you to save time, reduce cost & errors and increase efficiency. Refer to this link to learn more about pricing details.
 
Refer to my previous article to learn how to perform the following activities:
  • Create an Automation Account
  • Create Credential Asset – To store the credentials which will be used by PowerShell for authentication.
  • Import PowerShell Module – Import Microsoft Teams PowerShell Cmdlets module in order to access Teams Cmdlets.
  • Create PowerShell runbook – Script to create a new team
  • Test and publish the runbook
In this article, you will see how to provision a team using PowerShell runbook which will be called by webhook from Power Automate when users submit the request in the SharePoint list.
 
A webhook allows an external service to start a particular runbook in Azure Automation through a single HTTP request. Refer to this link to learn more about the automation webhook.
 

Design Flow

 
How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate 
 
You will see how to perform the following activities:
  • Create a SharePoint list – Custom list with default Title field and it contains the Team name that needs to be created.
  • Create PowerShell runbook
  • Create a webhook
  • Create a flow in Power Automate– It gets triggered when an item is created in SharePoint list and call the HTTP action.
  • Test the flow

Create a SharePoint list

  1. Navigate to SharePoint Online site.
  2. Create a custom list named as Microsoft Teams Requests.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate

Create PowerShell runbook

 
Azure Automation supports several types of runbooks. Se are going to see how we can create the PowerShell runbook in order to provision a team in Microsoft Teams.
 
Refer to this link to understand more about runbook types.
  1. Navigate to Automation account.
  2. Under Process Automation, click Runbooks.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Click Create a runbook.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Enter the required details and click

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. In the Edit pane, copy and paste the below script.
    1. Param  
    2. (  
    3.     [Parameter (Mandatory = $false)]  
    4.     [object] $WebhookData  
    5. )  
    6.  
    7. # If runbook was called from Webhook, WebhookData will not be null.  
    8. if ($WebhookData) {  
    9.     # Collect properties of WebhookData  
    10.     $WebhookName = $WebHookData.WebhookName  
    11.     $WebhookHeaders = $WebHookData.RequestHeader  
    12.     $WebhookBody = $WebHookData.RequestBody  
    13.     $Input = (ConvertFrom-Json -InputObject $WebhookBody)  
    14. }  
    15. else  
    16. {  
    17.     Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop  
    18. }  
    19.  
    20. # Get the team name from JSON input  
    21. $teamName=$Input.TeamName  
    22.  
    23. # Get the credential from Automation  
    24. $credential = Get-AutomationPSCredential -Name "TeamsAdminCredential"  
    25. $userName = $credential.UserName  
    26. $securePassword = $credential.Password  
    27.   
    28. $psCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $userName, $securePassword  
    29.  
    30. # Connect to Microsoft Teams  
    31. Connect-MicrosoftTeams -Credential $psCredential  
    32.  
    33. # Create a new Team  
    34. New-Team -DisplayName $teamName  
    35.  
    36. # Disconnect from Microsoft Teams  
    37. Disconnect-MicrosoftTeams  
  1. Click Save.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Publish the runbook.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Runbook successfully published, as shown below.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate

Create a webhook

  1. Navigate to Automation account.
  2. Under Process Automation, click Runbooks.
  3. Click the newly created runbook (named as CreateTeam).
  4. Click Add webhook.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Click Create new webhook. Enter the name and copy the URL (You might be able to see this once the webhook has been created). Click Ok.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Click Parameters and run settings and click Ok. Click Create. Webhook created successfully.

Create a flow in Power Automate

  1. Navigate to the Power Automate portal.
  2. Create a new flow and select the trigger as When an item is created. Enter the name for the flow and click Create.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Select the site address and list name for the trigger, as shown below.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Add HTTP action.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Enter the webhook URI which was copied while creating the webhook. Add the JSON to the Body, as shown below.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Save the flow.

Test the flow

  1. Navigate to SharePoint Online list. Create a new item in the list.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Under Run history, you can see the last run and click on it. Flow ran successfully, as shown below.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. HTTP Request:

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. From the body, copy the Job Id which will be useful in case if you need to find the status of the job.
  2. Navigate to Automation Account in Azure Portal.
  3. Under Process Automation, click Runbooks.
  4. Click the newly created runbook (named as CreateTeam).
  5. Under Resources, click Jobs and you can see the job status. You can also search the job based on the Job Id which can be copied from the flow HTTP action (Outputs -> Body).

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. Click on the job to see all the required details.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate
  1. The team was created successfully, as shown below.

    How To Start A PowerShell Runbook By A Webhook In Azure Automation Using Power Automate

Summary

 
In this blog, you saw how to start a PowerShell runbook by a webhook in Azure Automation using Power Automate.


Similar Articles