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:
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

  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. # If runbook was called from Webhook, WebhookData will not be null.
    7. if ($WebhookData) {
    8. # Collect properties of WebhookData
    9. $WebhookName = $WebHookData.WebhookName
    10. $WebhookHeaders = $WebHookData.RequestHeader
    11. $WebhookBody = $WebHookData.RequestBody
    12. $Input = (ConvertFrom-Json -InputObject $WebhookBody)
    13. }
    14. else
    15. {
    16. Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
    17. }
    18. # Get the team name from JSON input
    19. $teamName=$Input.TeamName
    20. # Get the credential from Automation
    21. $credential = Get-AutomationPSCredential -Name "TeamsAdminCredential"
    22. $userName = $credential.UserName
    23. $securePassword = $credential.Password
    24. $psCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $userName, $securePassword
    25. # Connect to Microsoft Teams
    26. Connect-MicrosoftTeams -Credential $psCredential
    27. # Create a new Team
    28. New-Team -DisplayName $teamName
    29. # Disconnect from Microsoft Teams
    30. 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.