-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSAdaptiveCard.psm1
369 lines (302 loc) · 12.7 KB
/
PSAdaptiveCard.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<#
.SYNOPSIS
Module for creating and managing Adaptive Cards for Microsoft Teams.
.DESCRIPTION
This module provides functions to generate JSON payloads for Adaptive Cards, Fact Sets, and Tables and more.
These payloads can be used to post messages to Microsoft Teams channels via Workflow webhooks.
.AUTHOR
EW
.COPYRIGHT
No
.LICENSE
MIT
.VERSION
0.0.6
.NOTES
- Requires PowerShell 5.1 or later.
- For more information, see the Adaptive Cards documentation at https://adaptivecards.io.
.EXAMPLE
$cardContent = @(
New-TextBlock -Text "Hello, Teams!"
)
New-AdaptiveCard -BodyContent $cardContent | ConvertTo-Json -Depth 20
#>
function New-AdaptiveCard {
param (
[Parameter(Mandatory = $true)]
[array]$BodyContent
)
$adaptiveCard = [pscustomobject]@{
type = 'AdaptiveCard'
body = @()
'$schema' = 'http://adaptivecards.io/schemas/adaptive-card.json'
version = '1.4'
}
foreach ($item in $BodyContent) {
$adaptiveCard.body += $item
}
return $adaptiveCard
}
function New-TextBlock {
param (
[Parameter(Mandatory = $false)]
[bool]$isSubtle,
[Parameter(Mandatory = $false)]
[bool]$separator,
[Parameter(Mandatory = $false)]
[int]$maxLines,
[Parameter(Mandatory = $false)][ValidateSet('default', 'small', 'medium', 'large', 'extraLarge', IgnoreCase = $false)]
[string]$size,
[Parameter(Mandatory = $false)][ValidateSet('default', 'lighter', 'bolder', IgnoreCase = $false)]
[string]$weight,
[Parameter(Mandatory = $false)]
[bool]$wrap,
[Parameter(Mandatory = $false)][ValidateSet('default', 'dark', 'light', 'accent', 'good', 'warning', 'attention', IgnoreCase = $false)]
[string]$color,
[Parameter(Mandatory = $false)][ValidateSet('default', 'monospace', IgnoreCase = $false)]
[string]$fontType,
[Parameter(Mandatory = $false)][ValidateSet('left', 'center', 'right', IgnoreCase = $false)]
[string]$horizontalAlignment,
[Parameter(Mandatory = $false)][ValidateSet('default', 'none', 'small', 'medium', 'large', 'extraLarge', 'padding', IgnoreCase = $false)]
[string]$spacing,
[Parameter(Mandatory = $true)]
[string]$text
)
begin {
$textBlock = [pscustomobject]@{
type = 'TextBlock'
text = $text
}
}
process {
if ($isSubtle) { $textBlock | Add-Member -NotePropertyName 'isSubtle' -NotePropertyValue $isSubtle }
if ($separator) { $textBlock | Add-Member -NotePropertyName 'separator' -NotePropertyValue $separator }
if ($maxLines) { $textBlock | Add-Member -NotePropertyName 'maxLines' -NotePropertyValue $maxLines }
if ($size) { $textBlock | Add-Member -NotePropertyName 'size' -NotePropertyValue $size }
if ($weight) { $textBlock | Add-Member -NotePropertyName 'weight' -NotePropertyValue $weight }
if ($wrap) { $textBlock | Add-Member -NotePropertyName 'wrap' -NotePropertyValue $wrap }
if ($color) { $textBlock | Add-Member -NotePropertyName 'color' -NotePropertyValue $color }
if ($fonttype) { $textBlock | Add-Member -NotePropertyName 'fonttype' -NotePropertyValue $fonttype }
if ($horizontalAlignment) { $textBlock | Add-Member -NotePropertyName 'horizontalAlignment' -NotePropertyValue $horizontalAlignment }
if ($spacing) { $textBlock | Add-Member -NotePropertyName 'spacing' -NotePropertyValue $spacing }
}
end {
return $textBlock
}
}
function New-FactSet {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject]$object,
[Parameter(Mandatory = $true)]
[string]$titleProperty,
[Parameter(Mandatory = $true)]
[string]$valueProperty
)
begin {
$facts = @()
}
process {
$value = [string]$object.$valueProperty
$fact = @{
title = $object.$titleProperty
value = $value
}
$facts += $fact
}
end {
$factSet = @{
type = 'FactSet'
facts = $facts
}
return $factSet
}
}
function New-Table {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject]$object,
[Parameter(Mandatory = $false, ParameterSetName = 'Highlight')]
[string]$highlightValueMatch,
[Parameter(Mandatory = $false, ParameterSetName = 'Highlight')][ValidateSet('dark', 'light', 'accent', 'good', 'warning', 'attention', IgnoreCase = $false)]
[string]$highlightValueStyle,
[Parameter(Mandatory = $false)]
[bool]$firstRowAsHeader = $true,
[Parameter(Mandatory = $false)][ValidateSet('default', 'dark', 'light', 'accent', 'good', 'warning', 'attention', IgnoreCase = $false)]
[string]$headerRowStyle,
[Parameter(Mandatory = $false)]
[bool]$showGridLines = $true,
[Parameter(Mandatory = $false)][ValidateSet('default', 'dark', 'light', 'accent', 'good', 'warning', 'attention', IgnoreCase = $false)]
[string]$gridStyle = 'default',
[Parameter(Mandatory = $false)][ValidateSet('left', 'center', 'right', IgnoreCase = $false)]
[string]$horizontalCellContentAlignment,
[Parameter(Mandatory = $false)][ValidateSet('top', 'center', 'bottom', IgnoreCase = $false)]
[string]$verticalCellContentAlignment
)
begin {
$table = [pscustomobject]@{
type = 'Table'
gridStyle = $gridStyle
firstRowAsHeader = $firstRowAsHeader
showGridLines = $showGridLines
columns = @()
rows = @()
}
# Add optional attributes if provided
if ($horizontalCellContentAlignment) {
$table.horizontalCellContentAlignment = $horizontalCellContentAlignment
}
if ($verticalCellContentAlignment) {
$table.verticalCellContentAlignment = $verticalCellContentAlignment
}
$columns = @()
$isHighlighting = $highlightValueMatch -and $highlightValueStyle
}
process {
if ($columns.Count -eq 0) {
# Get the noteproperties from the first object
$columns = $Object.PSObject.Properties | Where-Object { $_.MemberType -eq 'NoteProperty' } | Select-Object -ExpandProperty Name
# Add correct number of columns (one per property)
foreach ($column in $columns) {
$table.columns += @{
width = 1 # 'auto' worked fine before, but now results in unaligned tables in teams channels, soo.. 1.
}
}
# Add the header row
$headerRow = @{
type = 'TableRow'
cells = @()
}
# Add optional attributes if provided
if ($headerRowStyle) {
$headerRow.style = $headerRowStyle
}
# Add the header row
foreach ($column in $columns) {
$headerRow.cells += @{
type = 'TableCell'
items = @(
@{
type = 'TextBlock'
text = $column # Noteproperty names
}
)
}
}
$table.rows += $headerRow
}
# Process each object and add a row to the table
$row = @{
type = 'TableRow'
cells = @()
}
foreach ($column in $columns) {
$textValue = [string]$Object.$column
$textBlock = @{
type = 'TextBlock'
text = $textValue
}
if ($isHighlighting -and $textValue -match $highlightValueMatch) {
$textBlock.color = $highlightValueStyle
}
$row.cells += @{
type = 'TableCell'
items = @($textBlock)
}
}
$table.rows += $row
}
end {
return $table
}
}
function New-Image {
param (
[Parameter(Mandatory = $true)]
[string]$url,
[Parameter(Mandatory = $false)]
[string]$altText = 'image', #Yes, this is mandatory in spec, but getting errors from not providing alt-texts is not amusing, let's be honest.
[Parameter(Mandatory = $false)]
[string]$backgroundColor,
[Parameter(Mandatory = $false)][ValidateScript({$_ -ceq 'auto' -or $_ -ceq 'stretch' -or $_ -match "^\d+px$"}, ErrorMessage = "Height must be either lowercase 'auto', 'stretch', or a number followed by 'px'.")]
[string]$height = "auto",
[Parameter(Mandatory = $false)][ValidateSet('left', 'center', 'right', IgnoreCase = $false)]
[string]$horizontalAlignment,
[Parameter(Mandatory = $false)][ValidateSet('auto', 'stretch', 'small', 'medium', 'large', IgnoreCase = $false)]
[string]$size,
[Parameter(Mandatory = $false)][ValidateSet('default', 'person', IgnoreCase = $false)]
[string]$style,
[Parameter(Mandatory = $false)][ValidatePattern("^\d+(px)?$", ErrorMessage = "Width must be an integer, optionally followed by 'px' to specify this unit.")]
[string]$width,
[Parameter(Mandatory = $false)][ValidateSet('default', 'none', 'small', 'medium', 'large', 'extraLarge', 'padding', IgnoreCase = $false)]
[string]$spacing = 'default',
[Parameter(Mandatory = $false)]
[bool]$separator
)
begin {
$imageObject = [PSCustomObject]@{
type = 'Image'
url = $url
altText = $altText
}
}
process {
if ($backgroundColor) { $imageObject | Add-Member -NotePropertyName 'backgroundColor' -NotePropertyValue $vackgroundColor }
if ($height -ne 'auto') { $imageObject | Add-Member -NotePropertyName 'height' -NotePropertyValue $height }
if ($horizontalAlignment) { $imageObject | Add-Member -NotePropertyName 'horizontalAlignment' -NotePropertyValue $horizontalAlignment }
if ($size) { $imageObject | Add-Member -NotePropertyName 'size' -NotePropertyValue $size }
if ($style) { $imageObject | Add-Member -NotePropertyName 'style' -NotePropertyValue $style }
if ($width) { $imageObject | Add-Member -NotePropertyName 'width' -NotePropertyValue $width }
if ($spacing -ne 'default') { $imageObject | Add-Member -NotePropertyName 'spacing' -NotePropertyValue $spacing }
if ($separator) { $imageObject | Add-Member -NotePropertyName 'separator' -NotePropertyValue $separator }
}
end {
return $imageObject
}
}
function Send-JsonToTeamsWebhook {
param (
[Parameter(Mandatory = $true)]
[string]$webhookURI,
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[pscustomobject]$adaptiveCard,
[Parameter(Mandatory = $false)]
[switch]$fullWidth,
[Parameter(Mandatory = $false)]
[switch]$onlyConvertToJson
)
$attachment = [pscustomobject]@{
contentType = 'application/vnd.microsoft.card.adaptive'
contentUrl = $null
content = $adaptiveCard
}
$message = [pscustomobject]@{
type = 'message'
attachments = @()
}
$message.attachments += $attachment
if ($fullWidth) {
$msteamsProperty = @{
width = 'Full'
}
$message.attachments[0].content | Add-Member -MemberType NoteProperty -Name msteams -Value $msteamsProperty
}
$json = ($message | ConvertTo-Json -Depth 20) -replace '\\\\', '\' #-replace "\\", '\'
if ($onlyConvertToJson) {
Write-Output $json
Break
}
$parameters = @{
"URI" = $webhookURI
"Method" = 'POST'
"Body" = $json
"ContentType" = 'application/json; charset=UTF-8'
"ErrorAction" = 'Stop'
}
try {
Invoke-RestMethod @parameters
}
catch {
Write-Error "Failed to send request: $($_.Exception.Message)"
}
}