How can I send a message to microsoft teams channel with powershell, without using a webook?

Aaron Dalla-Longa 0 Reputation points
2023-07-03T23:00:20.8733333+00:00

Webhooks do not seem to work with our teams instance no matter what permissions we try:User's image

Therefore, I am wanting to do this via powershell. I have created an app registration in the azure tenant, and given it the following permissions:

User's imageThis is my current code:

## I am getting the application ID from the 'Application (client) ID' field in the app registration ##
$ApplicationID = $appID

## I am getting the tenant name from the 'Directory (tenant) ID' field in the app registration ##
$TenantDomainName = $tenant

## The access secret is the 'Value' of the secret for this app registration ##
$AccessSecret = $secret

## The team ID is taken from the URL of the teams link, that is after the 'group=' in the link ##
$TeamID = $team

## The channel ID is taken from the URL of the channel link, that is between the /channel and <CHANNEL NAME> of the share link, ends in .tacv2 ##
$ChannelID = $Chan

$Body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantDomainName/oauth2/v2.0/token" -Method POST -Body $Body
$accessToken = $ConnectGraph.access_token
$URLchatmessage="https://graph.microsoft.com/v1.0/teams/$TeamID/channels/$ChannelID/messages"
$headers = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type"  = "application/json"
}
$messageBody = @{
    body = @{
        content = "Test Message from CS API"
    }
} | ConvertTo-Json

Invoke-RestMethod -Method POST -Uri $URLchatmessage -Body $messageBody -Headers $headers

I am getting a 401 error when trying to run this code.

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,617 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,431 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,064 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,325 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,116 Reputation points
    2023-07-04T14:03:53.8833333+00:00

    Hello Aaron,

    Thank you for your question and for reaching out with your question today.

    A 401 error in this context typically indicates an authentication issue. The error suggests that the provided access token is not valid or lacks the necessary permissions to perform the desired action. To troubleshoot the 401 error, here are some steps you can follow:

    1. Check the application registration permissions:
      • Ensure that the application registration in Azure has the necessary permissions to access Microsoft Graph API. Specifically, make sure it has the ChatMessage.Send permission (delegated or application permission depending on your scenario) to send messages to Teams channels. You may need to update the application's API permissions in Azure AD and then grant admin consent.
    2. Verify the application's secret (client secret):
      • Ensure that the Client_Secret in your PowerShell script is correct and corresponds to the secret of the application registration. If you suspect the secret is incorrect, generate a new secret for the application and update your script accordingly.
    3. Validate the $accessToken value:
      • Print out the $accessToken value before making the API call to check if it contains a valid access token. You can verify it using an online JWT token decoder or other methods available.
    4. Check the application's permission grant:
      • Ensure that the application has been granted the required permissions by an administrator in the tenant. You can go to the Azure portal, navigate to "Azure Active Directory" > "App registrations" > "Your App Name" > "API permissions," and verify that the required permissions are granted.
    5. Test the access token:
      • You can use tools like Postman or the Microsoft Graph Explorer to test the access token and see if it works. If it works fine in these tools but not in PowerShell, it might indicate an issue in your PowerShell script.
    6. Ensure correct URLs and IDs:
      • Double-check the URLs and IDs used in your script (e.g., $TeamID, $ChannelID). Ensure they are accurate and correspond to the correct Teams team and channel.
    7. Confirm the application is authorized for the tenant:
      • Make sure the application registration is authorized for the tenant where your Teams instance is located.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.


  2. Meghana-MSFT 3,846 Reputation points Microsoft Vendor
    2023-07-04T15:23:49.7966667+00:00

    Sending message to a channel is not supported with application permissions, it is only supported in delegated context. Application permissions are only supported for migration.

    Please refer these documents to send message to a channel using Graph in delegated context -

    1. https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=powershell#tabpanel_1_powershell
    2. https://learn.microsoft.com/en-us/powershell/microsoftgraph/get-started?view=graph-powershell-1.0

    User's image