Microsoft Teams Automation - Create Team from Webhook

Today Microsoft released the Microsoft Teams Powershell Cmdlets module (https://www.powershellgallery.com/packages/MicrosoftTeams/0.9.0). This allows you to use PowerShell as the first basic API for Teams. Bill Bliss (@bill_bliss) from the Teams Team (still awkward wording) told us this week on Monday in a session at Microsoft Vienna that they will push the PowerShell Cmdlets in the coming weeks and the Graph API will follow. To be honest didn’t believe that they would be that fast in releasing the bits but I’m more than happy now. To take this a little further I created an Azure Automation Runbook for the CreateTeams Cmdlet in my Azure Tenant. The Runbook itself can be triggered by a webhook. With that in hand, you basically can create a bot or a Teams app to create or update your Teams based on the cmdlets Microsoft released.   The Runbook I created uses the basic steps for reading input from the webhook based on this blog post: https://blogs.technet.microsoft.com/stefan_stranger/2017/03/18/azure-automation-runbook-webhook-lesson-learned/``` [CmdletBinding()] Param ([object]$WebhookData) #this parameter name needs to be called WebHookData otherwise the webhook does not work as expected. $VerbosePreference = ‘continue’

region Verify if Runbook is started from Webhook.

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

# Collect individual headers. Input converted from JSON.
$DisplayName = $WebhookHeaders.DisplayName
$Input = (ConvertFrom-Json -InputObject $WebhookBody)
Write-Verbose "WebhookBody: $Input"
Write-Output -InputObject ('Runbook started from webhook {0} by {1}.' -f $WebhookName, $From)

} else { Write-Error -Message ‘Runbook was not started from Webhook’ -ErrorAction stop }

endregion

$AzureOrgIdCredential = “YOUR_CREDENTIAL_VAR”

$Cred = Get-AutomationPSCredential -Name $AzureOrgIdCredential

Connect-MicrosoftTeams -Credential $Cred

New-Team -DisplayName $DisplayName The $AzureOrgIdCredential variable holds a reference to the credentials you can create in the Azure Automation portal. That helps in getting rid of hard-coded credentials in your Runbooks.  All this does is waiting to be called with a variable DisplayName in the webhook header. An example call of this would look like this: $webhookurl = ‘https://s2events.azure-automation.net/webhooks?token=[YOUR_WEBHOOK_TOKEN]’

$body = @{“Classification” = “MBI”}

$params = @{ ContentType = ‘application/json’ Headers = @{‘DisplayName’ = ‘DEMO’} Body = ($body | convertto-json) Method = ‘Post’ URI = $webhookurl }

Invoke-RestMethod @params -Verbose