Tag Archives: Powershell

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

Domain Joins from PowerShell

I have been working in Kaseya a bit and I have had issues with domain joining systems and rejoining systems to domains. I would have even liked to rename systems if I could. After searching and testing many different ideas I have com across PowerShell’s way of domain joining systems.

Help for Add-Computer in Powershell

In Powershell V2 there is a new cmdlet called Add-Computer and it has more power than the GUI version of the domain joining system.
PS C:\> Add-Computer [-DomainName]  [-Credential ] [-OUPath ] [-PassThru] [-Server] [-Unsecure] [-Confirm]
PS C:\> Add-Computer [-WorkGroupName] [-Credential ]

The command below I will be joining the domain “testdom.local” and have already said what account I want to use to do this. This will result in the need to enter a password but that is a simple step. The -passthru switch will give me some basic details about the domain join.
PS C:\> Add-Computer -domainname testdom.local -cred testdom.local\administrator -passthru
You can even specify an Orginization Unit using the switch -OUPath if you did not want to move the system to a different OU after it has been joined to the domain.

The place that this is cmdlet great for anyone running a managed service system like Kaseya is when you can remove a computer from a domain and change the computers name and rejoin the domain. To change a systems name you can use the following command.
PS C:\> Add-Computer -workgroup workgroup -newname testsys01 -force -restart
The switch -Force will suppress the confirmation boxes since the cmdlet Add-Computer asks for confirmation on all commands. The final switch will -Restart will do exactly what it says, it will restart the system after it has run since a restart is often required to make the changes effective.