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
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
- Navigate to SharePoint Online site.
- Create a custom list named as Microsoft Teams Requests.
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.
- Navigate to Automation account.
- Under Process Automation, click Runbooks.
- Click Create a runbook.
- Enter the required details and click
- In the Edit pane, copy and paste the below script.
- Param
- (
- [Parameter (Mandatory = $false)]
- [object] $WebhookData
- )
-
- # 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
- $Input = (ConvertFrom-Json -InputObject $WebhookBody)
- }
- else
- {
- Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
- }
-
- # Get the team name from JSON input
- $teamName=$Input.TeamName
-
- # Get the credential from Automation
- $credential = Get-AutomationPSCredential -Name "TeamsAdminCredential"
- $userName = $credential.UserName
- $securePassword = $credential.Password
-
- $psCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $userName, $securePassword
-
- # Connect to Microsoft Teams
- Connect-MicrosoftTeams -Credential $psCredential
-
- # Create a new Team
- New-Team -DisplayName $teamName
-
- # Disconnect from Microsoft Teams
- Disconnect-MicrosoftTeams
- Click Save.
- Publish the runbook.
- Runbook successfully published, as shown below.
Create a webhook
- Navigate to Automation account.
- Under Process Automation, click Runbooks.
- Click the newly created runbook (named as CreateTeam).
- Click Add webhook.
- 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.
- Click Parameters and run settings and click Ok. Click Create. Webhook created successfully.
Create a flow in Power Automate
- Navigate to the Power Automate portal.
- Create a new flow and select the trigger as When an item is created. Enter the name for the flow and click Create.
- Select the site address and list name for the trigger, as shown below.
- Add HTTP action.
- Enter the webhook URI which was copied while creating the webhook. Add the JSON to the Body, as shown below.
- Save the flow.
Test the flow
- Navigate to SharePoint Online list. Create a new item in the list.
- Under Run history, you can see the last run and click on it. Flow ran successfully, as shown below.
- HTTP Request:
- From the body, copy the Job Id which will be useful in case if you need to find the status of the job.
- Navigate to Automation Account in Azure Portal.
- Under Process Automation, click Runbooks.
- Click the newly created runbook (named as CreateTeam).
- 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).
- Click on the job to see all the required details.
- The team was created successfully, as shown below.
Summary
In this blog, you saw how to start a PowerShell runbook by a webhook in Azure Automation using Power Automate.