Webhooks, which send data in JSON format over HTTP, are ideal for informing systems about events. This can be used, for example, to notify Microsoft Teams about events and store them in an action card. Actions for problem solving can also be included.

Webhooks offer special benefits in combination with Microsoft Teams or other collaboration tools. Teams can be divided into channels so that notifications can be sorted according to category or origin. In addition, admins can comment on events there and clarify who is responsible for certain incidents.

Action cards in Microsoft Teams

In the article "Instead of email alerts: Send system notifications to Microsoft Teams via webhooks," I describe how to send message cards to Teams via PowerShell. This article, on the other hand, deals with so-called action cards. Not only do they store information, but they also enable you to trigger actions using buttons.

I will demonstrate this feature using a practical use case: A PowerShell script gathers the utilization data of drives on selected servers, checks how much storage is still available there (in percentage), and sends an alert to Teams as an action card via webhook if the threshold value is exceeded.

Action card in Microsoft Teams which not only displays info about the drive but can also start WAC via a button

Action card in Microsoft Teams which not only displays info about the drive but can also start WAC via a button

A button then opens the server in WAC so that you can fix the problem by increasing the partition size.

Technical implementation

Action cards are declared via a JSON file and sent over an HTTP post to the Teams Connector URL. I cover the configuration of a Teams Connector for webhooks in the article above. The same steps are required for action cards.

The standardized JSON format, in conjunction with an HTTP request, allows you to use webhooks independently of the underlying platform. Hence, besides PowerShell, curl and other services can also be used to send the webhook.

PowerShell provides cmdlets to create and edit JSON files, but creating an action card with them is relatively cumbersome. Therefore, the PowerShell community has developed a special module for this purpose: PSTeams. The following how-to uses this module.

Explanation of the PowerShell script

Before the actual design of the action card, various inputs must be gathered. The data include the servers to be queried, a threshold that triggers the alert, the Teams connector ID, and the URL of your WAC.

Any number of additional computers may be added to the array of computer names. For a larger number, a CSV import into an array is recommended.

# Computers to be tested
$TargetComputers = @("Server01", "Server02")
# Free memory in percent, from which an alert is to be triggered
$Threshold = 60
# TeamsID = URL generated by the connector
$TeamsID = ""
# URL of WAC
$WACURL = "<https://localhost:6516/>"

Note: 60 is very high as a threshold and is only used to trigger an alert for demonstration purposes. A practical value here would be 10 to 20 percent free drive storage.

The script triggers an alert for drive C as soon as less than 60 percent of disk space is free

The script triggers an alert for drive C as soon as less than 60 percent of disk space is free

The following script ensures that the necessary module is installed and imported into the system:

# ModuleCheck: Installs PSTeams if not already available
try {
 Get-InstalledModule PSTeams
}
catch {
 Install-Module -Name PSTeams -Force
}
finally {
 Import-Module PSTeams
}
Now all drive information must be read on the server and cached in variables:
# Query the disk information
$Disks = Get-WmiObject Win32_LogicalDisk -ComputerName $TargetComputer
| Select-Object DeviceID, Size,FreeSpace

# Iterate over the specified computers
foreach ($Disk in $Disks){
  # Determine the drive letter
  $DiskName = $Disk.DeviceID
  # Size of the drive in GibiByte
  [double]$DiskSpaceGiB = $Disk.Size / (1024 * 1024 * 1024) 
  $DiskSpaceGiB = [math]::Round($DiskSpaceGiB,2)
  # Free Storage in GibiByte
  [double]$DiskFreeSpaceGiB = $Disk.FreeSpace / (1024 * 1024 * 1024) 
  $DiskFreeSpaceGiB = [math]::Round($DiskFreeSpaceGiB,2)
  # Free storage in percent with two decimal places
  [double]$DiskFreePercentage = $DiskFreeSpaceGiB / $DiskSpaceGiB 
  $DiskFreePercentage = [math]::Round($DiskFreePercentage,4) * 100
}

All the desired disk information is now stored in corresponding variables. $DiskFreePercentage, whose value results from the ratio between free and total storage, now contains the free disk space in percent. Based on this, it determines whether an alert is to be generated.

# If the free storage (percent) falls below the threshold, a Teams alert is sent
if ($DiskFreePercentage -lt $Threshold){
 $WACURLServer = $WACURL + "computerManagement/connections/computer/" + $TargetComputer + "/tools/storage/storage/volumes"

 # Create the card elements
 $Button1 = New-TeamsButton -Name 'Open Client Storage Options' -Link $WACURLServer
 $Fact1 = New-TeamsFact -Name "Server" -Value "**$($TargetComputer)**"
 $Fact2 = New-TeamsFact -Name "Partition" -Value "**$($DiskName)**"
 $Fact3 = New-TeamsFact -Name "Space Total (GibiByte)" -Value "**$($DiskSpaceGiB)**"
 $Fact4 = New-TeamsFact -Name "Space Free (GibiByte)" -Value "**$($DiskFreeSpaceGiB)**"
 $Fact5 = New-TeamsFact -Name "Space Free (Percent)" -Value "**$($DiskFreePercentage)**"

 # Create a section and assign the card elements
 $Section = New-TeamsSection -Buttons $Button1 `
   -ActivityDetails $Fact1, $Fact2, $Fact3, $Fact4, $Fact5
 
 # Send the card
 Send-TeamsMessage -URI $TeamsID -Color Red -Sections $Section `
 -MessageTitle "Alert: Drive $DiskName on $TargetComputer has only $DiskFreePercentage % left!"
}

If the determined percentage falls below the defined threshold, an action card is created via the cmdlets of the PSTeams module. This is made up of facts and a button. The facts contain the properties of the affected drive. The button opens the storage management for the server in WAC. This link is created directly under the IF condition. The objects are combined into a section and transferred to the specified URL via Send-TeamsMessage. The already action card will appear in the Teams channel.

The full script is below:

# Zu Prüfende Computer
$TargetComputers = @("Server01", "Server02")
# Freier Speicher (Prozent), ab welchem alarmiert werden soll
$Threshold = 60
# TeamsID - URL, welche vom Connector generiert wurde
$TeamsID = ""
# URL des Windows Admin Centers
$WACURL = "<https://localhost:6516/>"

# ModuleCheck: Installiert PSTeams, falls noch nicht vorhanden
try {
  Get-InstalledModule PSTeams
}
catch {
  Install-Module -Name PSTeams -Force
}
finally {
  Import-Module PSTeams
}

# Iterieren über die angegebenen Computer
foreach ($TargetComputer in $TargetComputers){
    # Abfrage der Disk-Informationen
    $Disks = Get-WmiObject Win32_LogicalDisk -ComputerName $TargetComputer 
| Select-Object DeviceID, Size,FreeSpace

    # Loop über die angegebenen Laufwerke
    foreach ($Disk in $Disks){
        # Ermitteln des Laufwerksbuchstaben
        $DiskName = $Disk.DeviceID
        # Größe des Laufwerks in GibiByte
        [double]$DiskSpaceGiB = $Disk.Size / (1024 * 1024 * 1024) 
        $DiskSpaceGiB = [math]::Round($DiskSpaceGiB,2)
        # Freier Speicher in GibiByte
        [double]$DiskFreeSpaceGiB = $Disk.FreeSpace / (1024 * 1024 * 1024) 
        $DiskFreeSpaceGiB = [math]::Round($DiskFreeSpaceGiB,2)
        # Freie Speicher in Prozent mit zwei Nachkommastellen
        [double]$DiskFreePercentage = $DiskFreeSpaceGiB / $DiskSpaceGiB 
        $DiskFreePercentage = [math]::Round($DiskFreePercentage,4) * 100

        # Wenn freier Speicher (Prozent) den Threshold unterschreitet, wird ein Teams-Alert gesendet
        if ($DiskFreePercentage -lt $Threshold){
          $WACURLServer = $WACURL + "computerManagement/connections/computer/" + $TargetComputer + "/tools/storage/storage/volumes"

          # Erstellen der Card-Elemente
          $Button1 = New-TeamsButton -Name 'Open Client Storage Options' -Link $WACURLServer
          $Fact1 = New-TeamsFact -Name "Server" -Value "**$($TargetComputer)**"
          $Fact2 = New-TeamsFact -Name "Partition" -Value "**$($DiskName)**"
          $Fact3 = New-TeamsFact -Name "Space Total (GibiByte)" -Value "**$($DiskSpaceGiB)**"
          $Fact4 = New-TeamsFact -Name "Space Free (GibiByte)" -Value "**$($DiskFreeSpaceGiB)**"
          $Fact5 = New-TeamsFact -Name "Space Free (Percent)" -Value "**$($DiskFreePercentage)**"

          # Erstellen ein Section und Zuordnung der Card-Elemente
          $Section = New-TeamsSection `
              -Buttons $Button1 `
              -ActivityDetails $Fact1, $Fact2, $Fact3, $Fact4, $Fact5
            
          # Senden der Card
          Send-TeamsMessage -URI $TeamsID -Color Red -Sections $Section `
           -MessageTitle "Alert: Drive $DiskName on $TargetComputer -> has only $DiskFreePercentage % left!" `
        }
    }
}

Conclusion

With PSTeams, admins have an easy-to-use tool at hand to generate alerts, combined with an option for action. Of course, such a script could then be run automatically at regular intervals via a cron job or task scheduler.

The example shows a simple application, but there are certainly many more use cases. For example, various RSS feeds of IT security news could be received via PowerShell and collected in Teams.

In addition to simply retrieving data from a website, the button can also send HTTP POST requests; this opens up almost unlimited possibilities. Alerts or messages could be forwarded by team members to other systems by clicking a button.

Subscribe to 4sysops newsletter!

In this way, tickets could be opened or closed, messages could be saved in your own to-do list, and virtual machines could be started or stopped in the cloud or on-premises. All tasks that should not be fully automated, but rather executed in a controlled manner, can thus achieve partial automation by using approval steps via Teams.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account