From a76a6707373681a17c0a7a0ffe7a610e85b00c81 Mon Sep 17 00:00:00 2001 From: ITS-s-autowinpkg Date: Mon, 2 Sep 2024 17:14:50 +0200 Subject: [PATCH] added new MS Teams Workflow --- WASP/Private/Invoke-Webhook.ps1 | 77 +++++++++++++++++++-------- WASP/TeamsWebhook.json | 93 ++++++++++++++++++++++++++------- 2 files changed, 131 insertions(+), 39 deletions(-) diff --git a/WASP/Private/Invoke-Webhook.ps1 b/WASP/Private/Invoke-Webhook.ps1 index 4a5cb71..28261ab 100644 --- a/WASP/Private/Invoke-Webhook.ps1 +++ b/WASP/Private/Invoke-Webhook.ps1 @@ -8,8 +8,8 @@ function Invoke-Webhook { FileName: Invoke-Webhook.ps1 Author: Uwe Molnar Contact: its-wcs-ma@unibas.ch - Created: 2022-06-07 - Version: 1.0.0 + Created: 2024-09-02 + Version: 1.1.0 #> [CmdletBinding()] param ( @@ -21,48 +21,83 @@ function Invoke-Webhook { $WebhookURL = $config.Application.TeamsWebhook $system = $config.Application.System $WebHookTemplate = $config.Application.WebhookTemplate + + # Function to add a new table row + function Add-TableRow { + param ( + [string]$packageName, + [string]$version + ) + + return @{ + type = "TableRow" + cells = @( + @{ + type = "TableCell" + items = @( + @{ + type = "TextBlock" + text = "$packageName" + wrap = $true + } + ) + }, + @{ + type = "TableCell" + items = @( + @{ + type = "TextBlock" + text = "$version" + wrap = $true + } + ) + } + ) + } + } + } process { - [System.Collections.ArrayList]$NewPackages = @() - + $JSONBody = Get-Content $WebHookTemplate -Raw | ConvertFrom-Json + $table = $JSONBody.attachments[0].content.body | Where-Object {$_.type -eq 'Table'} + foreach ($Package in $Packages) { $PackageName = $Package.name $PackageVersion = $Package.version - $null = $NewPackages.Add("$PackageName $PackageVersion") + + $addRow = Add-TableRow -packageName $PackageName -version $PackageVersion + + $table[0].rows += $addRow } - # Formatting for a nicer look in MS Teams - $pacakgesEdited = foreach ($Package in $NewPackages){$Package.Insert($Package.Length, "`n`n")} + $updatedJsonContent = $JSONBody | ConvertTo-Json -Depth 12 - # Get the Webhook-Template and add the necessary fields: system, color and packages + # Update the JSONBody with the correct System and Style $systemRegEx = 'Insert system here' - $colorRegEx = 'Insert color here' - $packagesRegEx = 'Insert packages here' - $color = '' + $styleRegEx = 'Insert style here' + $style = '' switch ($system) { "Test-System" { - $color = "attention" # red + $style = "attention" # orange } "Prod-System" { - $color = "good" # green + $style = "good" # green } } - $JSONBody = Get-Content $WebHookTemplate - $JSONBodyNew = $JSONBody - $JSONBodyNew = $JSONBodyNew -replace $systemRegEx, $system -replace $packagesRegEx, $pacakgesEdited -replace $colorRegEx, $color + $updatedJsonContent = $updatedJsonContent -replace $systemRegEx, $system -replace $styleRegEx, $style $parameters = @{ - "URI" = $WebhookURL - "Method" = 'POST' - "Body" = $JSONBodyNew - "ContentType" = 'application/json' + "URI" = $WebhookURL + "Method" = 'POST' + "Body" = $updatedJsonContent + "ContentType" = 'application/json' } try { - Invoke-RestMethod @parameters + Invoke-Webrequest @parameters Write-Log "Info-Message successfully send via Webhook to Microsoft Teams." -Severity 1 } catch { diff --git a/WASP/TeamsWebhook.json b/WASP/TeamsWebhook.json index 097142c..ee10b50 100644 --- a/WASP/TeamsWebhook.json +++ b/WASP/TeamsWebhook.json @@ -4,38 +4,95 @@ { "contentType": "application/vnd.microsoft.card.adaptive", "contentUrl": null, - "content": - { + "content": { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", - "version": "1.5", - "msteams": { - "width": "Full" - }, + "version": "1.2", "body": [ { "type": "TextBlock", - "text": "New Software-Package(s) available [Insert system here]", - "wrap": true, - "color": "Insert color here", - "size": "large", - "weight": "bolder" + "size": "Medium", + "weight": "Bolder", + "text": "WASP-Framework", + "style": "heading", + "wrap": true }, { "type": "ColumnSet", "columns": [ { "type": "Column", + "width": "auto", + "items": [ + { + "type": "Image", + "style": "Person", + "url": "https://cdn.pixabay.com/photo/2013/07/12/16/29/hornet-151003_1280.png", + "altText": "SWD", + "size": "Small" + } + ] + }, + { + "type": "Column", + "width": "stretch", "items": [ { "type": "TextBlock", - "text": "Insert packages here", - "isVisible": true, - "id": "packages", - "size": "Medium" + "weight": "Bolder", + "text": "New Software-Package(s) available [Insert system here]", + "wrap": true, + "spacing": "None", + "separator": true, + "horizontalAlignment": "Left", + "height": "stretch", + "color": "Insert style here" + } + ] + } + ] + }, + { + "type": "Table", + "columns": [ + { + "width": 1 + }, + { + "width": 1 + } + ], + "rows": [ + { + "type": "TableRow", + "cells": [ + { + "type": "TableCell", + "style": "Insert style here", + "items": [ + { + "type": "TextBlock", + "text": "Software", + "wrap": true, + "style": "heading", + "size": "Medium" + } + ] }, - ], - "width": "stretch" + { + "type": "TableCell", + "style": "Insert style here", + "items": [ + { + "type": "TextBlock", + "text": "Version", + "wrap": true, + "style": "heading", + "size": "Medium" + } + ] + } + ] } ] } @@ -43,4 +100,4 @@ } } ] -} +} \ No newline at end of file