ExampleJSON.ps1
|
#Enter your webhook URI. This will be generated when you add a workflow to your teams-channel and select the template "post-to-a-channel-when-a-webhook-request-is-received". #Read more here: https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/ $URI = 'https://prod-140.westeurope.logic.azure.com:443/workflows/[REDACTED]/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=[REDACTED]' #Region Table #In this example we are using Get-Service, but any object can be used $TableObject = Get-Service | select Status, Name, DisplayName -first 7 $TableProperties = $TableObject | Get-Member -MemberType NoteProperty #Counting the properties in object and add/prepare a column for each property $columns = @() $column = @{width = 'auto'} $TableProperties.Name | % { $columns += $column } #Creating first row using the names of object properties as values inside cells. This will be the table header. $rows = @() $cells = @() #Creating the cells and adding values $TableProperties.Name | % { Clear-Variable TableCellItem, Name -ErrorAction SilentlyContinue $Name = $_ $TableCellItem = [ordered] @{ type = "TextBlock" text = "$Name" wrap = $true weight = "Bolder" #Bold - used to highlight table headers } $cell = [ordered] @{ type = 'TableCell' items = @( $TableCellItem ) } $cells += $cell } #Adding the cells to the table row $TableRow = [ordered] @{ type = 'TableRow' cells = @( $cells ) style = 'accent' #Accent style - used to highlight table headers } $rows += $TableRow #Now we need to add the actual values for each item in PS-object. Same procedure as above, but with values instead of property-names, and without header styling. $TableObject | % { $cells = @() Clear-Variable CurrentItem -ErrorAction SilentlyContinue $CurrentItem = $_ $TableProperties.Name | % { Clear-Variable TableCellItem, Name -ErrorAction SilentlyContinue $Name = $_ $TableCellItem = [ordered] @{ type = 'TextBlock' text = "$($CurrentItem | select -ExpandProperty $_)" color = 'default' } #Uncomment to format based on status value (running vs. stopped) #if ($Name -eq 'Status'){ # if (($CurrentItem | select -ExpandProperty $_) -ne 'Running') { # $TableCellItem.Color = 'attention' # } # else { # $TableCellItem.Color = 'good' # } #} $cell = [ordered] @{ type = 'TableCell' items = @( $TableCellItem ) } $cells += $cell } $TableRow = [ordered] @{ type = 'TableRow' cells = @( $cells ) #style = 'accent' } $rows += $TableRow } #Then we add the collums and rows to a table $Table = [ordered] @{ type = 'Table' gridStyle = 'accent' columns = @( $columns ) rows = @( $rows ) } #EndregionTable #Region TitleAndText $Header = [ordered] @{ type = 'TextBlock' text = '**Header ✅**' #The "**" On both sides will make this text bold. This is due to markup interpetation on adaptive cards. Emojis pass thru fine. style = 'heading' } $Text = [ordered] @{ type = 'TextBlock' text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' style = 'default' wrap = 'true' } #Endregion TitleAndText #Region OrderAndContentTypes #The content is added to the body, and the body is added to the adaptive-card wrapper with content types, schema and version etc. $body = @() $Body += $Header $Body += $Text $body += $Table $attachments = [ordered] @{ contentType = 'application/vnd.microsoft.card.adaptive' contentUrl = $null content = @{ '$schema' = 'http://adaptivecards.io/schemas/adaptive-card.json' type = 'AdaptiveCard' version = '1.4' body = @( $body ) } } $Complete = [ordered] @{ type = 'message' attachments = @( $attachments ) } #Endregion OrderAndContentTypes #The resulting object is converted to JSON and posted to the webhook-URL $Json = $Complete | ConvertTo-Json -Depth 20 $Json $Json | clip.exe break $parameters = @{ "URI" = $URI "Method" = 'POST' "Body" = $Json "ContentType" = 'application/json; charset=UTF-8' } Invoke-RestMethod @parameters |