How to Create a Team in Microsoft Teams Using Powershell in Azure Automation

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

  1. Navigate to the Azure portal.
  2. Click on Create a Resource.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Search for Automation and select Automation. Click Create.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Enter all the required details and click Create.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Once it is deployed successfully, navigate to the resource.
  2. 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.
  1. Navigate to Automation account created in the previous step.
  2. Under Shared Resources, click Credentials.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Click Add a credential.
  2. Enter all the required details and click Create.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Credential assets created successfully. Copy the name in a notepad which will be used in the PowerShell to get the credentials.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation

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.
  1. Navigate to Automation account.
  2. Under Shared Resources, click Modules.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Click Browse Gallery. Search for Teams and select MicrosoftTeams

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Click Import to import the Teams PowerShell Module.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. It might take a few minutes to import the module.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Once the module is imported successfully, all the available cmdlets will be displayed.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation

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.
  1. Navigate to Automation account.
  2. Under Process Automation, click Runbooks.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Click Create a runbook.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Enter the required details 

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. In the Edit pane, copy and paste the below script.
    1. # Get the credential from Automation  
    2. $credential = Get-AutomationPSCredential -Name 'TeamsAdminCredential'  
    3. $userName = $credential.UserName  
    4. $securePassword = $credential.Password  
    5.   
    6. $psCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $userName, $securePassword  
    7.  
    8. # Connect to Microsoft Teams  
    9. Connect-MicrosoftTeams -Credential $psCredential  
    10.  
    11. # Create a new Team  
    12. New-Team -DisplayName "Demo Team - AZ Automation"  
    13.  
    14. # Disconnect from Microsoft Teams  
    15. Disconnect-MicrosoftTeams  
  1. Click Save.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation

Test and publish the runbook

  1. Navigate to Automation account.
  2. Under Process Automation, click Runbooks.
  3. Click the newly created runbook (named as CreateTeam).
  4. Click Edit.
  5. Click Test pane to test the runbook.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Click Start.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Script ran successfully, as shown below.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. The team was created successfully, as shown below.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Close this window by clicking the close (X) icon at the top right corner of the page.
  2. Publish the runbook.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. Runbook successfully published, as shown below.

    How To Create A Team In Microsoft Teams Using Powershell In Azure Automation
  1. 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.