Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new MS Teams Workflow #33

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions WASP/Private/Invoke-Webhook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
93 changes: 75 additions & 18 deletions WASP/TeamsWebhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,100 @@
{
"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"
}
]
}
]
}
]
}
]
}
}
]
}
}
Loading