PowerShell Display Table In HTML Email
I think this is a better solution (see ref 1)
$DataSet.Tables[0] |select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray | ConvertTo-Html
With the ExcludeProperty we can all exclude the noisy columns from our table without the need of specify specialy what columns we want it
(1) http://sqlblog.com/blogs/jonathan_kehayias/archive/2010/05/25/reinventing-the-wheel-automating-data-consistency-checks-with-powershell.aspx
I use like this:
Import-Module C:\dbatools\dbatools.psd1
$HostName = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
$DiskResult = Get-DbaDiskSpace -ComputerName $HostName | Sort-Object $_.FreeInGB | Out-DbaDataTable
# Create column headers of Table1
$HtmlTable1 = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #FFFFFF'>
<th align=left><b>ComputerName</b></th>
<th align=left><b>Name</b></th>
<th align=left><b>Label</b></th>
<th align=left><b>SizeInGB</b></th>
<th align=left><b>FreeInGB</b></th>
<th align=left><b>PercentFree</b></th>
</tr>"
# Insert data into Table1
foreach ($row in $DiskResult)
{
$HtmlTable1 += "<tr style='font-size:13px;background-color:#FFFFFF'>
<td>" + $row.ComputerName + "</td>
<td>" + $row.Name + "</td>
<td>" + $row.Label + "</td>
<td>" + $row.SizeInGB + "</td>
<td>" + $row.FreeInGB + "</td>
<td>" + $row.PercentFree + "</td>
</tr>"
}
$HtmlTable1 += "</table>"
# Send Mail Inputs
$smtpserver = "smtp.domain.com"
$from = "Powershell Mail <" + $HostName + "@domain.com>"
$to = "<[email protected]>", "<[email protected]>"
$cc = "<[email protected]>" , "<[email protected]>"
$subject = "Disk Check for SQL Server"
$body = "Disk info for SQL Server like below:<br/><br/>" + $HtmlTable1
Send-MailMessage -smtpserver $smtpserver -from $from -to $to -cc $cc -subject $subject -body $body -bodyashtml
Here's how I'd do it:
# Create a DataTable
$table = New-Object system.Data.DataTable "TestTable"
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn Dept,([string])
$table.columns.add($col1)
$table.columns.add($col2)
# Add content to the DataTable
$row = $table.NewRow()
$row.Name = "John"
$row.Dept = "Physics"
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Name = "Susan"
$row.Dept = "English"
$table.Rows.Add($row)
# Create an HTML version of the DataTable
$html = "<table><tr><td>Name</td><td>Dept</td></tr>"
foreach ($row in $table.Rows)
{
$html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td></tr>"
}
$html += "</table>"
# Send the email
$smtpserver = "smtpserver.domain.com"
$from = "[email protected]"
$to = "[email protected]"
$subject = "test"
$body = "Hi there,<br />Here is a table:<br /><br />" + $html
Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml
You can try (not tested):
$message.Body = $message.Body + ($DataSet.Tables | format-Table -auto | convertto-html)