GitHub Icon Image
GitHub

Retrieve Message Center announcements and post to MS Teams channel

Summary

This script allows you to connect to your SharePoint Online tenant and retrieve Message Center announcements. It then connects to Microsoft Teams and loops through the announcements, posting them to a specific Teams channel.

Example Screenshot

Implementation

  • Create csv file with the list of site collection URLs to enable app catalog
  • Open Windows PowerShell ISE
  • Create a new file
  • Copy the code below
  • Save the file and run it
  • PnP PowerShell
  • CLI for Microsoft 365 with PowerShell
# Connect to SharePoint Online
Connect-PnPOnline -Url https://yourTenantName.sharepoint.com/ -Interactive 

# Get Message Center announcements
$announcements = Get-PnPMessageCenterAnnouncement | Where-Object { $_.Category -eq "PlanForChange" } | Select-Object Title, Description

# Connect to teams
Connect-MicrosoftTeams 

# Loop through the announcements and post to Teams channel
foreach ($announcement in $announcements) {
    $message = "$($announcement.Title): $($announcement.Description)"
    Submit-PnPTeamsChannelMessage -Team "TeamID" -Channel "General" -Message $message
}

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

# Example: .\Send-MessageCenterAnnouncementsToMSTeams.ps1 -Category "planForChange" -TeamName "Team Name" -ChannelName "Channel Name"

[CmdletBinding()]
param (
    [Parameter(Mandatory = $false, HelpMessage = "Specify category of the message center announcement")]
    [string]$Category = "planForChange",
    [Parameter(Mandatory = $true, HelpMessage = "Specify the display name of the team to which the channel belongs to")]
    [string]$TeamName,
    [Parameter(Mandatory = $true, HelpMessage = "Specify the display name of the channel to post the message center announcements")]
    [string]$ChannelName
)

begin {
    #Log in to Microsoft 365
    Write-Host "Connecting to Tenant" -f Yellow

    $m365Status = m365 status
    if ($m365Status -match "Logged Out") {
        m365 login
    }

    Write-Host "Connection Successful!" -f Green
}
process {
    # Get Message Center announcements
    $announcements = m365 tenant serviceannouncement message list --query "[?category == '$($Category)']" | ConvertFrom-Json
    Write-Host "Found $($announcements.Count) announcements to post to MS Teams channel"

    # Get information about Microsoft Teams team with name
    $teamInfo = m365 teams team get --name $TeamName | ConvertFrom-Json

    # Get information about Microsoft Teams team channel with name
    $channelInfo = m365 teams channel get --teamId $teamInfo.id --name $ChannelName | ConvertFrom-Json

    # Loop through the announcements and post to Teams channel
    foreach ($announcement in $announcements) {
        Write-Host "Sending message $($announcement.Title)..."

        $message = "$($announcement.Title): $($announcement.Description)"
        m365 teams message send --teamId $teamInfo.id --channelId $channelInfo.id --message $message
    }
}
end {
    m365 logout
    Write-Host "Finished" -ForegroundColor Green
}

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Contributors

Author(s)
Valeras Narbutas
Nanddeep Nachan

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI