Introduction

We wanted to receive an alert into a Microsoft Teams Channel we have setup for our Helpdesk Team when a device in Ninja RMM came back online.

This script can be loaded into Ninja RMM and set to run on an offline device.

When the device comes online, it’ll run and fire a notification into the Microsoft Teams Channel via an Incoming Webhook, like this:

Setup Incoming Webhook In Microsoft Teams

Follow the steps shown in this Microsoft Guide to Create an Incoming Webhook

Create an Incoming Webhook – Teams | Microsoft Learn

Setup Script In Ninja RMM

I’m going to assume you know how to add a Powershell script to Ninja RMM, so add the following ensuring you have the correct Ninja RMM instance and you put your Incoming Webhook URL in.

$hostname = hostname
$title = 'Device Online: ' + $hostname
$text = $hostname + ' at ' + $env:NINJA_LOCATION_NAME + ' (' + $env:NINJA_COMPANY_NAME + ') Is Online'
$ninjaUrl = 'https://eu.ninjarmm.com/#/deviceDashboard/' + $env:NINJA_AGENT_NODE_ID + '/overview'

$body = '{
   "@context": "https://schema.org/extensions",
   "@type": "MessageCard",
   "themeColor": "0072C6",
   "title": "' + $title + '",
   "text": "' + $text + '",
   "potentialAction": [
   {
      "@type": "OpenUri",
      "name": "Open in NinjaRMM",
      "targets": [
         { "os": "default", "uri": "' + $ninjaUrl +'" }
      ]
   }
   ]
}'

$uri = 'webhookurl'

Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'

Adding Button To Open Halo PSA Ticket

On the assumption that you want to be notified of a device turning on because of a ticket in Halo PSA, you can also add a button to the alert that’ll take you straight to the ticket, like this.

When you set the script to run on a device in Ninja RMM, you’ll be prompted to type in the Halo Ticket Id.

To use this version of the script, add an Integer script variable to the script in Ninja RMM, called “Halo Ticket Id” and make it mandatory.

Use the following script instead, again ensuring you have the correct Ninja RMM instance and you put your Incoming Webhook URL in.

$hostname = hostname $title = 'Device Online: ' + $hostname
$text = $hostname + ' at ' + $env:NINJA_LOCATION_NAME + ' (' + $env:NINJA_COMPANY_NAME + ') Is Online'
$ninjaUrl = 'https://eu.ninjarmm.com/#/deviceDashboard/' + $env:NINJA_AGENT_NODE_ID + '/overview'
$haloUrl = 'https://haloinstance/tickets?id=' + $env:haloTicketId

$body = '{
   "@context": "https://schema.org/extensions",
   "@type": "MessageCard",
   "themeColor": "0072C6",
   "title": "' + $title + '",
   "text": "' + $text + '",
   "potentialAction": [
   {
      "@type": "OpenUri",
      "name": "Open in NinjaRMM",
      "targets": [
         { "os": "default", "uri": "' + $ninjaUrl +'" }
      ]
   },
   {
      "@type": "OpenUri",
      "name": "Open Ticket In Halo PSA",
      "targets": [
         { "os": "default", "uri": "' + $haloUrl +'" }
      ]
   }
   ]
}'

$uri = 'webhookurl'

Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'