Send Monitoring Alerts To Microsoft Teams

When you have a large monitoring setup you need to know when something important is failing or is just offline. If you have setup Microsoft Teams you can send a message to a channel to let the support team repair the service or device with minimal down time.

I have set this up in Labtech to send alerts to the alerts channel. Labtech has the nice ability to inject data in a powershell script before it is run, but we will be demonstrating the script by running it directly in powershell.

First step is to create a webhook in the MS Teams channel you wish to use. At the time of writing you can only create a webhook from the web version of MS Teams, it will not work in the desktop client.

Open the connectors in the channel you want to use for your alerts.

Open the connectors for the channel

Find the “Incoming Webhook” and add it.

Add the Incoming Webhook

Next we need to name the connector and assign an avatar, these settings will change how the sender of the message appears in the channel. I recommend setting the name to the service that will send the messages, and the image to the systems icon or something that is attention grabbing.

Set the name and image the webhook will use when posting.

Once done you will get a url, save a copy of the URL as it is important to the next step.

Copy the Webhook URL for your connector

The next step is to copy the following script, you can also download it from here https://github.com/nightred/MSTeams/blob/master/send-msteams-alert.ps1.

# Powershell MS Teams alert template
#
# This is a template to demonstrate the ability to send notices to
# Microsoft Teams from PowerShell
#
# Parse the command variables
param (
  [string]$message = "This is a test of the alert system",
  [string]$title = "Test Message",
  [string]$status = "Cleared",
  [string]$monitor = "Testing - monitor",
  [string]$location = "Downtown",
  [string]$system = "labsys01"
)
# Edit the URI with the webhook url that was provided
$uri = 'https://outlook.office365.com/webhook/MISSING';
# Build the message Body
$body = ConvertTo-Json -Depth 4 @{
  title = $title
  text = ' '
  sections = @(
    @{
      activityText = $message
    },
    @{
      facts = @(
        @{
          name = 'Status'
          value = $status
        },
        @{
          name = 'System Name'
          value = $system
        },
        @{
          name = 'Monitor'
          value = $monitor
        },
        @{
          name = 'Location'
          value = $location
        }
      )
    }
  )
}
#Send the message to MS Teams
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json';
Write-Output "INFO - Message has been sent.";

You will need to edit the $uri line to have the URL that was provided in the Webhook creation step.
$uri = 'https://outlook.office365.com/webhook/MISSING';

Once done you can execute the script to see the default alert message. To send your own values to the script you can pass variables when run like the following example.
send-msteams-alert.ps1 -message "System is offline" -title "Offline" -status "Critical" -monitor "Offine Check" -location "Downtown" -system "testLab01"

If everything was done correctly you should have a nice message posted to Microsoft Teams like the following.

Message sent to Microsoft Teams from a Webhook

6 thoughts on “Send Monitoring Alerts To Microsoft Teams

  1. Alec Beckett

    This looks great, just what i was looking for!

    I am relatively new to LT and was wondering where the powershell script would sit. Would be as an alert on the monitor itself? How would i then pass through the variables from the alert like the offline monitor alert for instance.

    Thanks!

    Reply
    1. nightred Post author

      In this case I run this command on the LT Server by changing the @computerid@ to 1 (or what ever your LT server’s id is) and then pasting this script in to the command ‘Execute Script’ type ‘Powershell Bypass’

      When doing this you can set the default values parameters to LabTech variables like %computername% instead of passing them as parameters.

      Reply
  2. Michael Kanet

    I’m very interested in getting this working. I’m not super experienced at scripting in LabTech and wanted to clarify a few things. I don’t see any variables for which monitor has failed and is running the script. How are you passing that information into the alert on MS Teams? Are you hard coding that into the script and running a different script for each different type of alert?

    Reply
    1. nightred Post author

      A monitor will pass the details in a field called @FIELDNAME@ to the script based on what is in the Identity Field for the monitor.
      Normally I would have the monitor run a script that validates the monitor and if it is valid then I would call the script to send a message to Teams. The Validation script would format a message and any variables to be passed on to the Teams script.

      Reply
  3. gicquel thierry

    Hello, it isn’t necessary to do this with a script because now you can generate an email attached to the channel ! In options in the channel (in the right top) click to 3 little dots and get an email address;

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.