Category Archives: Alerting

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