Use Webhook Connector to send data from PowerShell to Microsoft Teams


Yesterday we announced Microsoft Teams, a new chat-based workspace in Office 365. Microsoft Teams is an entirely new experience that brings together people, conversations and content—along with the tools that teams need—so they can easily collaborate to achieve more.

Check this video or the step-by-step introduction video to learn more.

One of the cool features is the connectors you can use with Microsoft Teams. Office 365 Connectors are a great way to get useful information and content into Microsoft Teams. Any user can connect their team to services like Trello, GitHub, Bing News, Twitter, etc., and get notified of the team’s activity in that service

image

Scroll through the Connectors and select the Incoming Webhook Connector

 

image

 

Give your Incoming Webhook Connector a name and upload an fitting icon and click create.

image

 

Copy the Incoming Webhook url to your clipboard, we will need that info in our PowerShell script.

image

 

PowerShell script

We now are going to call the Incoming Webhook from PowerShell.

[sourcecode language='powershell'  padlinenumbers='true']
# ---------------------------------------------------
# Script: C:\Users\stefstr\Microsoft\OneDrive - Microsoft\Scripts\PS\MicrosoftTeams\testwebhook_v2.ps1
# Version: 0.1
# Author: Stefan Stranger
# Date: 11/03/2016 10:48:58
# Description: Call Microsoft Teams Incoming Webhook from PowerShell
# Comments:
# Changes:  
# Disclaimer: 
# This example is provided “AS IS” with no warranty expressed or implied. Run at your own risk. 
# **Always test in your lab first**  Do this at your own risk!! 
# The author will not be held responsible for any damage you incur when making these changes!
# ---------------------------------------------------


$webhook = '[webhookurl]'

$Body = @{
        'text'= 'Hello World! from PowerShell'
}

$params = @{
    Headers = @{'accept'='application/json'}
    Body = $Body | convertto-json
    Method = 'Post'
    URI = $webhook 
}

Invoke-RestMethod @params
[/sourcecode]

If everything works ok, this will be the result.

msteamswebhook

Have fun using Microsoft Teams!

Comments (11)

  1. Joe says:

    Glad to see Teams released! Having some challenges with WebHooks. When I try to create one from my on-prem TFS 2015 (“build has completed” event), with the URL provided by Teams, my test results in a HTTP 400 error. When I use this PS script (with my URL) as an example, I get the following error:

    Invoke-RestMethod : The ‘accept’ header must be modified using the appropriate property or method.
    Parameter name: name
    At line:30 char:1
    + Invoke-RestMethod @params
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    Any suggestions?

    1. Hi Joe,

      How does your accept header look like?

      Maybe you can share you script without the webhook so I can have a look?

      /Stefan

      1. Joe says:

        Thanks for the quick reply – I used the $params assignment as in the powershell script above. When I remove the Headers assignment, the webhook worked great!

        However, I am still struggling with getting my on-prem TFS 2015 notifications to use the same webhook (on build failure, for example) – When I test the service hook from within TFS 2015, it yields a 400-Bad Request. Any ideas?

        1. Hi Joe,

          Maybe you can do some network tracing to find out how your web request message looks like? You could use Fiddler to see how your web request looks like.

          Hope this helps.

          Stefan

          1. Stephan says:

            Hi Stefan

            I’m trying to do pretty much the same with an on-premise instance of TFS, my example is just for a pull request but that should not matter

            Below is the message that it is trying to send as from the TFS test webhook popup when setting up a web hook:

            Method: POST
            URI: https://outlook.office365.com/webhook/
            HTTP Version: 1.1
            Headers:
            {
            Content-Type: application/json; charset=utf-8
            }
            Content:
            {
            “subscriptionId”: “00000000-0000-0000-0000-000000000000”,
            “notificationId”: 12,
            “id”: “2ab4e3d3-b7a6-425e-92b1-5a9982c1269e”,
            “eventType”: “git.pullrequest.created”,
            “publisherId”: “tfs”,
            “message”: {
            “text”: “Jamal Hartnett created a new pull request”,
            “html”: “Jamal Hartnett created a new pull request”,
            “markdown”: “Jamal Hartnett created a new pull request”
            },
            “detailedMessage”: {
            “text”: “Jamal Hartnett created a new pull request\r\n\r\n- Merge status: Succeeded\r\n- Merge commit: eef717(https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/eef717f69257a6333f221566c1c987dc94cc0d72)\r\n”,
            “html”: “Jamal Hartnett created a new pull request\r\n\r\nMerge status: Succeeded\r\nMerge commit: eef717\r\n”,
            “markdown”: “Jamal Hartnett created a new pull request\r\n\r\n+ Merge status: Succeeded\r\n+ Merge commit: [eef717](https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/eef717f69257a6333f221566c1c987dc94cc0d72)\r\n”
            },
            “resource”: {
            “repository”: {
            “id”: “4bc14d40-c903-45e2-872e-0462c7748079”,
            “name”: “Fabrikam”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079”,
            “project”: {
            “id”: “6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c”,
            “name”: “Fabrikam”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c”,
            “state”: “wellFormed”
            },
            “defaultBranch”: “refs/heads/master”,
            “remoteUrl”: “https://fabrikam.visualstudio.com/DefaultCollection/_git/Fabrikam”
            },
            “pullRequestId”: 1,
            “status”: “active”,
            “createdBy”: {
            “id”: “54d125f7-69f7-4191-904f-c5b96b6261c8”,
            “displayName”: “Jamal Hartnett”,
            “uniqueName”: “fabrikamfiber4@hotmail.com”,
            “url”: “https://fabrikam.vssps.visualstudio.com/_apis/Identities/54d125f7-69f7-4191-904f-c5b96b6261c8”,
            “imageUrl”: “https://fabrikam.visualstudio.com/DefaultCollection/_api/_common/identityImage?id=54d125f7-69f7-4191-904f-c5b96b6261c8”
            },
            “creationDate”: “2014-06-17T16:55:46.589889Z”,
            “title”: “my first pull request”,
            “description”: ” – test2\r\n”,
            “sourceRefName”: “refs/heads/mytopic”,
            “targetRefName”: “refs/heads/master”,
            “mergeStatus”: “succeeded”,
            “mergeId”: “a10bb228-6ba6-4362-abd7-49ea21333dbd”,
            “lastMergeSourceCommit”: {
            “commitId”: “53d54ac915144006c2c9e90d2c7d3880920db49c”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/53d54ac915144006c2c9e90d2c7d3880920db49c”
            },
            “lastMergeTargetCommit”: {
            “commitId”: “a511f535b1ea495ee0c903badb68fbc83772c882”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/a511f535b1ea495ee0c903badb68fbc83772c882”
            },
            “lastMergeCommit”: {
            “commitId”: “eef717f69257a6333f221566c1c987dc94cc0d72”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/eef717f69257a6333f221566c1c987dc94cc0d72”
            },
            “reviewers”: [
            {
            “reviewerUrl”: null,
            “vote”: 0,
            “id”: “2ea2d095-48f9-4cd6-9966-62f6f574096c”,
            “displayName”: “[Mobile]\\Mobile Team”,
            “uniqueName”: “vstfs:///Classification/TeamProject/f0811a3b-8c8a-4e43-a3bf-9a049b4835bd\\Mobile Team”,
            “url”: “https://fabrikam.vssps.visualstudio.com/_apis/Identities/2ea2d095-48f9-4cd6-9966-62f6f574096c”,
            “imageUrl”: “https://fabrikam.visualstudio.com/DefaultCollection/_api/_common/identityImage?id=2ea2d095-48f9-4cd6-9966-62f6f574096c”,
            “isContainer”: true
            }
            ],
            “commits”: [
            {
            “commitId”: “53d54ac915144006c2c9e90d2c7d3880920db49c”,
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/commits/53d54ac915144006c2c9e90d2c7d3880920db49c”
            }
            ],
            “url”: “https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079/pullRequests/1”
            },
            “createdDate”: “2016-11-22T06:27:44.3959863Z”
            }

  2. Hugo Conceicao says:

    I can’t see WebHook messages in mobile (Android). (capture: https://i.imgur.com/Mantb0Y.jpg)

    1. Hi Hugo,

      You need to provide a bit more info on what you are doing if you want me to help you 🙂

      Microsoft Teams is working on my Android device.

      /Stefan

      1. Hugo Conceicao says:

        Hi Stefan,

        I run your script(copy&paste) and works fine in Microsoft Teams desktop version, but on Android I can’t see the messages created from webhooks.

        I think the problem it’s related with text color, because if do “Copy message” the message is there.

        Cheers

        1. Hi Hugo,

          Maybe it’s something with the Android client for teams. Could try asking your question at UserVoice? https://officespdev.uservoice.com/

          Sorry for being able to help you further with this question.

          /Stefan

  3. Don says:

    Works like a charm! However, the PowerShell messages don’t show in the Windows Phone 10 Teams app, only replies to these messages.

    1. Hi Don,

      Looks like this is a bug at the Windows Phone app. According to the Desktop Team app the messages show up.

      Maybe you can post your question at Uservoice? https://officespdev.uservoice.com/

      Sorry I cannot really help you with this question.

      /Stefan

Skip to main content