-
Notifications
You must be signed in to change notification settings - Fork 100
/
CreateAdaptiveCard.ps1
72 lines (70 loc) · 3.09 KB
/
CreateAdaptiveCard.ps1
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
function New-AdaptiveCard {
param(
[Parameter(Position = 0, Mandatory = $false)] [psObject]$Columns,
[Parameter(Position = 1, Mandatory = $false)] [switch]$DontHideBody,
[Parameter(Position = 2, Mandatory = $false)] [Int]$ColorSwitchColumnNumber,
[Parameter(Position = 3, Mandatory = $false)] [psObject]$ColorSwitchHashTable,
[Parameter(Position = 4, Mandatory = $false)] [String]$ColorSwitchDefault,
[Parameter(Position = 5, Mandatory = $false)] [String]$originator
)
Process {
$AdaptiveCard = @{}
$AdaptiveCard.Add("version","1.0")
$AdaptiveCard.Add("type","AdaptiveCard")
if(!$DontHideBody.IsPresent){
$AdaptiveCard.Add("hideOriginalBody","true")
}
if([String]::IsNullOrEmpty($originator)){
$AdaptiveCard.Add("originator",$originator)
}
$Body = @()
$BodyHash = @{}
$BodyHash.Add("type","ColumnSet")
$ColumnSet = @()
if($Columns -ne $null){
$RowNumber = 0
foreach($ColumnRow in $Columns){
$ColumnNumber = 0
$ColumnRow.PSObject.Properties | ForEach-Object{
$CellNumber = 0
if($RowNumber -eq 0){
$ColumnItems = @()
$columnHeader = @{}
$columnHeader.Add("type", "TextBlock")
$columnHeader.Add("text", $_.Name)
$columnHeader.Add("size", "Large")
$columnHeader.Add("weight", "Bolder")
$columnHeader.Add("color", "Accent")
$ColumnItems += $columnHeader
$columnHash = @{}
$columnHash.Add("width","auto")
$columnHash.Add("type","Column")
$columnHash.Add("items",$ColumnItems)
$ColumnSet += $columnHash
}
$columnCell = @{}
$columnCell.Add("type", "TextBlock")
if($CellNumber -eq 0){
$columnCell.Add("weight", "Bolder")
}
if($ColumnNumber -eq $ColorSwitchColumnNumber){
if($ColorSwitchHashTable.Contains($_.Value)){
$columnCell.Add("color", $ColorSwitchHashTable[$_.Value])
}else{
$columnCell.Add("color", $ColorSwitchDefault)
}
}
$columnCell.Add("text", $_.Value)
$ColumnSet[$ColumnNumber]["items"] += $columnCell
$ColumnNumber++
}
$RowNumber++
}
}
$BodyHash.Add("columns",$ColumnSet)
$Body += $BodyHash
$AdaptiveCard.Add("body",$Body)
$convertedBody = ConvertTo-Json $AdaptiveCard -Depth 9
return $convertedBody
}
}