Skip to content

Instantly share code, notes, and snippets.

@mbourgon
Last active December 10, 2019 19:51
Show Gist options
  • Save mbourgon/82d329a1bb5e39046ba6626c09fd9a13 to your computer and use it in GitHub Desktop.
Save mbourgon/82d329a1bb5e39046ba6626c09fd9a13 to your computer and use it in GitHub Desktop.
Queries SQL Server, and uses the HTML that it produced and pastes it to teams.
<#you can't use ACTUAL adapter cards, but you can use HTML, albeit just BODY.#>
<#Thebakingdba.blogspot.com 2019#>
. C:\Powershell_Scripts\invoke-sqlcmd2.ps1
$query = @"
DECLARE @tableHTML NVARCHAR(MAX)
, @MailSubject VARCHAR(200)
SELECT @tableHTML = '<table>'
+ N'<th>database_id</th>'
+ N'<th>db_name</th>'
+ N'<th>create_date</th>'
+ N'<th>recovery_model</th>'
+ N'<th>recovery_model_desc</th>'
+ CAST((
SELECT top 5
td = ISNULL(CONVERT(VARCHAR(20),database_id),'')
, '' , td = ISNULL(name,'')
, '' , td = ISNULL(CONVERT(VARCHAR(20),create_date,120),'')
, '' , td = CONVERT(VARCHAR(1),recovery_model)
, '' , td = CONVERT(VARCHAR(60),recovery_model_desc)
FROM sys.databases
ORDER BY database_id
FOR XML PATH('tr') , TYPE ) AS NVARCHAR(MAX)) + N'</table>' ;
SELECT @tableHTML as tablehtml
"@
$queryresults = Invoke-Sqlcmd2 -ServerInstance myservername -Database mydbname -query $query
$webhook_URL = "https://outlook.office.com/webhook/......"
$json = @"
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"text": "$($queryresults.tablehtml)",
"version": "1.0",
"type": "AdaptiveCard"
}
"@
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body $json -Uri $webhook_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment