共用方式為


Creating a Teams "New Channel" notification

One of my customers recently asked for a solution to checking a particular Microsoft Team multiple times a day for new channel additions.  In their organization, someone is responsible for creating a new channel every time new item for review is published, and then all of the communications, files, and data related to that item is stored in that particular time.

So, this seemed like a good time for some automation.

Building on this post I did a while ago for getting started with Azure Automation Runbooks, we can modify the scripting and the module and come out with a solution pretty quickly.  Read through that quick, and then come back here.

Done?

So, interject these pieces in at the applicable points in time, and you're good to go.

[toc]

Getting Information on the Team you want to monitor

We don't supply a whole lot of detail in the Get-Team cmdlet.  We only expose the DisplayName and GroupID (guid) of an object.  In this script, what we're going to do is look at the channels for a team, check the description for a particular phrase or keyword, and if it doesn't exist, we're going to treat it as new.

  1. To install the Teams module, launch an elevated PowerShell session and run Install-Module MicrosoftTeams to download and install it from the PowerShell Gallery.
  2. Once you've installed the module, you can use the Connect-MicrosoftTeams cmdlet to connect to your Office 365 tenant.
  3. In order to get the information for the team whose channels we want to monitor, we'll need the object ID.  Run the Get-Team cmdlet, and then note the GroupID.

Configuring Azure Automation for Microsoft Teams

  1. Create an Office 365 Service Account that will be used to log in and run the services.  If you want this account to send mail, it will need an Exchange Online mailbox license assigned.

  2. Launch the Azure Portal, and create an Azure Automation service account.

  3. Under Shared Resources, click Modules, click Browse gallery.

  4. Search for Teams and select it.

  5. Click Import.

  6. Click OK to acknowledge.

  7. Hopefully, you are blessed with an Import Successful toast notification.  If it doesn't, do not pass go and do not collect $200.

  8. Add a credential (it'll be the credentials of the service account that you created in step 1).  Pay attention to the  *Name value, as that's what we'll reference later on in the script.

  9. Prepare your script.  In normal Windows PowerShell scripting, you may be familiar with the Get-Credential cmdlet, which gives you the ability to request and store a credential object (so aptly named).  In PowerShell Runbooks, we're going to use the Azure Automation version of this cmdlet (Get-AutomationPSCredential).  When we use it, we're going to tell it which Azure Automation credential to use (using the value in the name parameter specified earlier).
    $Credential = Get-AutomationPSCredential -Name "O365Service"

  10. Under Process Automation, click Create a runbook.  You can see an overview of the process here.

  11. Fill out the RunBook properties and click Create.

  12. Paste the script content in the runbook and click Test pane to try it out.
    I've highlighted the areas you'll want to update:

     $Credential = Get-AutomationPSCredential -Name "O365Service"
    Connect-MicrosoftTeams -Credential $Credential
    $GroupId = " <Group ID from above> "
    $NewChannels = Get-TeamChannel -GroupId $GroupId | ? { ($_.Description -eq $null -or $_.Description -notlike " *RunBook* ") -and $_.DisplayName -ne "General" }
    $SmtpServer = "smtp.office365.com"
    $SmtpPort = "587"
    $To = "recipient@domain.com"
    $From = "sender@domain.com"
    foreach ($Channel in $NewChannels)
    {
        $Date = Get-Date -Format "yyyy-MM-dd HH:MM:ss"
        If ($Channel.Description) { $Description = $Channel.Description + " Processed via RunBook at $($Date) " }
        Else { $Description = "Processed via RunBook at $($Date) " }
        Set-TeamChannel -GroupId $GroupId -CurrentDisplayName $Channel.DisplayName -Description $Description
        Send-MailMessage -UseSsl -Port 587 -Credential $Credential -SmtpServer $smtpserver -To $To -From $From -Subject "New channel $($Channel.DisplayName) has been added. Go take a look!"
    }
    Disconnect-MicrosoftTeams
    
  13. Click Start.

  14. Wait for the test to complete and review the output.

  15. Publish it.

Once you have your schedule set, you'll begin receiving notifications on a regular basis for Team Channel additions.