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.
Use case/Business Scenario
In the case that we need to restrict the users by creating teams on their own and need to set up some provisioning process around this, then we need to go with either PowerShell or Graph API. We might need to schedule this as a job that will get all the requests (new Team requests) raised by the users and provision the teams accordingly after some approval process. For this, we can use Azure Automation to schedule the job which executes the PowerShell script periodically to provision the teams.
Azure Automation helps you to save time, reduce cost & errors and increase efficiency. Refer to this
link to learn more about pricing details.
In this article, you will see how to perform the following tasks:
- 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
Create an Automation Account
- Navigate to the Azure portal.
- Click on Create a Resource.
- Search for Automation and select Automation. Click Create.
- Enter all the required details and click Create.
- Once it is deployed successfully, navigate to the resource.
- Refer to this link to understand more about the Automation account.
Create credential asset
In order to run the PowerShell runbook, we need the credentials such as a user name and password for authentication. These security credentials can be stored in an Azure Automation. Refer to this
link to learn more about credential assets in Azure Automation.
- Navigate to Automation account created in the previous step.
- Under Shared Resources, click Credentials.
- Click Add a credential.
- Enter all the required details and click Create.
- Credential assets created successfully. Copy the name in a notepad which will be used in the PowerShell to get the credentials.
Import PowerShell Modules
In order to create a team in Microsoft Teams using PowerShell, we need to install the Microsoft Teams Cmdlets module. Azure Automation provides the ability to import PowerShell modules into your Automation Account to be used by the PowerShell based runbooks.
- Navigate to Automation account.
- Under Shared Resources, click Modules.
- Click Browse Gallery. Search for Teams and select MicrosoftTeams
- Click Import to import the Teams PowerShell Module.
- It might take a few minutes to import the module.
- Once the module is imported successfully, all the available cmdlets will be displayed.
Create PowerShell runbook
Azure Automation supports several types of runbooks. We 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
- In the Edit pane, copy and paste the below script.
- # 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 "Demo Team - AZ Automation"
-
- # Disconnect from Microsoft Teams
- Disconnect-MicrosoftTeams
- Click Save.
Test and publish the runbook
- Navigate to Automation account.
- Under Process Automation, click Runbooks.
- Click the newly created runbook (named as CreateTeam).
- Click Edit.
- Click Test pane to test the runbook.
- Click Start.
- Script ran successfully, as shown below.
- The team was created successfully, as shown below.
- Close this window by clicking the close (X) icon at the top right corner of the page.
- Publish the runbook.
- Runbook successfully published, as shown below.
- Based on the requirements, this PowerShell runbook can be scheduled by clicking Link to Schedule.
Summary
Thus, in this blog, you saw how to create a team in Microsoft Teams using PowerShell in Azure Automation.