Skip to content

Instantly share code, notes, and snippets.

@hospitableit
Last active May 11, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hospitableit/ded1ea72574c53c70f6da1d6817b8423 to your computer and use it in GitHub Desktop.
Save hospitableit/ded1ea72574c53c70f6da1d6817b8423 to your computer and use it in GitHub Desktop.
PRTG WebEx Teams Notification
#PRTG WebEx Teams Notification
#Mandatory Parameters from Command Line are the Space Name and the Message
Param (
[Parameter(Mandatory=$True)][string]$Space,
[Parameter(Mandatory=$True)][string]$Message
)
#Set Constants
$AccessToken = "Access Token from Cisco for your Bot"
$QueryToken = "Bearer" + " " + $AccessToken
$SpaceAPIURL = "https://api.ciscospark.com/v1/rooms"
$MessageAPIURL = "https://api.ciscospark.com/v1/messages"
$DebugLogFilePath = "c:\temp\WebExNotifier_DEBUG.log"
# Disable SSL certificate checking so we can connect to servers using a cert signed by a private CA and enable TLS1.2
Add-Type @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
"@
[ServerCertificateValidationCallback]::Ignore();
#[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Debug Logging Function
Function Write-DebugLog {
Param (
[Parameter(Mandatory=$True)]
[string]
$DebugMessage
)
$DebugStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$DebugLine = "$DebugStamp $DebugMessage"
Add-Content $DebugLogFilePath -value $DebugLine
}
#API Request Function
Function Send-APIRequest {
Param (
[Parameter(Mandatory=$True)]
[hashtable]
$APIQuery
)
Invoke-WebRequest @APIQuery -DisableKeepAlive
}
#Get list of Rooms/Spaces that Bot user is a member of to extract ID and title
Function Get-WebExSpaceIDs {
$SpaceIdList = @()
$WebExSpaceIdsQuery = @{uri = $SpaceAPIURL;
Method = 'GET';
Headers = @{'Accept' = 'application/json'; 'Authorization' = $QueryToken;}
}
$SpaceIdQueryResultJSON = Send-APIRequest $WebExSpaceIdsQuery
$SpaceIdQueryResult = ConvertFrom-Json -InputObject $SpaceIdQueryResultJSON
$SpaceIdQueryResult.items | ForEach-Object {
$SpaceIdCandidate = New-Object psobject
$SpaceIdCandidate | Add-Member -MemberType NoteProperty -Name Space -Value $_.title
$SpaceIdCandidate | Add-Member -MemberType NoteProperty -Name SpaceId -Value $_.id
$SpaceIdList += $SpaceIdCandidate
}
$SpaceIdList
}
#Send WebEx Teams Message
Function Send-TeamsMessage {
Param (
[Parameter(Mandatory=$True)]
[string]
$TeamsSpaceID,
[Parameter(Mandatory=$True)]
[string]
$TeamsMessage
)
$TeamsMessageBody = @"
{
"roomId": "$TeamsSpaceID",
"text": "$TeamsMessage"
}
"@
$TeamsMessageQuery = @{uri = $MessageAPIURL;
Method = 'POST';
Headers = @{'Accept' = 'application/json'; 'Content-Type' = 'application/json'; 'Authorization' = $QueryToken;}
Body = $TeamsMessageBody
}
$MessageQueryResultJSON = Send-APIRequest $TeamsMessageQuery
}
#Main
try {
$SpaceList = Get-WebExSpaceIDs
}
catch {
Write-DebugLog "Space ID Query Error"
$err=$_
Write-DebugLog $err.Exception
Exit
}
#Check if Space Parameter passed to script exists in $SpaceIdList
try {
if ( $SpaceList.Space -contains $Space) {
$SpaceId = $SpaceList.Spaceid[$SpaceList.Space.IndexOf($Space)]
#$SpaceId
}
else {
#Space Lookup Error
Write-DebugLog "Space Lookup Failed - Invalid Input"
Exit
}
}
catch {
Write-DebugLog "Space Wrangling Error"
$err=$_
Write-DebugLog $err.Exception
Exit
}
#Check Message Length (Maximum is 7439 Bytes)
try {
$MessageSize = [System.Text.Encoding]::UTF8.GetByteCount($Message)
}
catch {
Write-DebugLog "Message Size Error"
$err=$_
Write-DebugLog $err.Exception
Exit
}
try {
if ($MessageSize -lt 7439) {
#Send Message
Send-TeamsMessage -TeamsSpaceID $SpaceId -TeamsMessage $Message
}
else {
#Message too big error
Write-DebugLog "Message too big"
}
}
catch {
Write-DebugLog "Message Send Error"
$err=$_
Write-DebugLog $err.Exception
Exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment