You can send messages to Teams channels from PowerShell using a webhook or Microsoft Graph API calls. Let’s see how to send and read messages in Microsoft Teams channels using PowerShell. You can use these PS scripts in various monitoring or notification scenarios where you need to send notification not via email (Send-MailMessage cmdlet), but directly to a team channel.
material:
Send messages to Microsoft Teams using WebHook
You can send messages to the Microsoft Teams channel using the built-in WebHook connectors. The webhook connector is a URL address that you can send a JSON object to using an HTTP POST request.
- Create a channel in the team. You can do this using the Microsoft Teams PowerShell module. For example:
Get-team -DisplayName sysops| New-TeamChannel -DisplayName "AdminAlerts" -MembershipType Private
- Then open the Teams client (a desktop or web version) and select connectors in the context menu of the channel;
- add incoming webhook connector type;
- Specify the name of the connector;
- Copy the URL of the connector that Azure has created for you.
To send a simple message to the channel using this URL, run the following PowerShell command:
$myTeamsWebHook = “https://woshub.webhook.office.com/webhookb2/[email protected]/IncomingWebhook/xxxxxxxxxxxxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxxx/xxxxx-xxxx-xxxx-xxxx”
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Test Teams!"}' -Uri $myTeamsWebHook
Make sure your message is visible in the Teams channel. The connector you created is displayed as the author of the message.
MS Teams does not allow you to send more than 4 messages per second.
You can add other data to your team notification, change the font, color, and add additional information from your PowerShell scripts.
For example, the following script monitors a user lockout event (event 4740) on a domain controller with the PDC FSMO role and sends a notification to the team channel.
$LockedUser = Search-ADAccount -UsersOnly –LockedOut | Get-ADUser –Properties lockoutTime, emailaddress | Select-Object emailaddress, @{n='lockoutTime';e={[DateTime]::FromFileTime($_.lockoutTime)}} | Sort-Object LockoutTime -Descending | Select-Object -first 1
$myTeamsWebHook = "YOUR-WEBHOOK-URL"
$webhookMessage = [PSCustomObject][Ordered]@{
"@type" = "FF0000"
"@context" = "http://schema.org/extensions"
"summary" = "Locked User: $($LockedUser.SamAccountName) "
"themeColor" = '700015'
"title" = "User Lockout Event"
"text" = "`n
SamAccountName: $($LockedUser.SamAccountName)
Mail: $($LockedUser.EmailAddress)
Timestamp: $($LockedUser.LockoutTime.ToString()) "
}
$webhookJSON = convertto-json $webhookMessage -Depth 50
$webhookCall = @{
"URI" = $myTeamsWebHook
"Method" = 'POST'
"Body" = $webhookJSON
"ContentType" = 'application/json'
}
Invoke-RestMethod @webhookCall
The event then appears in the Teams channel and an administrator can respond to.
How to send or read team messages with Microsoft Graph API?
Using the Microsoft Graph API, you can send and read messages in the Teams channel. First, you need to register the Azure app, set the permissions (Group.Read.All
, ChannelMessage.Send
, Chat.ReadWrite
And ChatMessage.Send
), and obtain an authentication token (learn more in the article How to Connect to Azure Microsoft Graph API Using PowerShell).
$ApplicationID = "4434ad23-b212-3212-3aad-54321de3bbc"
$TenatDomainName = "26216542-aaaa-bbbb-2212-65566aa6c32"
$AccessSecret="12-32Bh654321d3-seLa23l33p.hhj33MM21aaf"
$Body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri -Method POST -Body $Body
$token = $ConnectGraph.access_token
$URLchatmessage="https://graph.microsoft.com/v1.0/teams/$TeamID/channels/$ChannelID/messages"
$BodyJsonTeam = @"
{
"body": {
"content": "Hello World"
}
}
"@
Invoke-RestMethod -Method POST -Uri $URLchatmessage -Body $BodyJsonTeam -Headers -Headers @{Authorization = "Bearer $($token)"}
you can get $TeamID
And $ChannelID
using the Get-Team
And Get-TeamChannel
From the MicrosoftTeams module.
Similarly, you can read messages from Teams chat using the GET method.
Leave a Comment