I have 2 scripts
script1 has the following:
$exportObject = New-Object System.Collections.ArrayList
$exportObject | Select-Object
in sc
You can be as creative as you like with HTML styling of course. Below code styles the table like this:
# just some fake data here
$exportObject = @(
[PSCustomObject]@{
'Server' = 'Server1.com'
'Cube' = 'Cube1'
'Connection Details' = 'Connection changed!'
},
[PSCustomObject]@{
'Server' = 'Server2.com'
'Cube' = 'Cube2'
'Connection Details' = 'Connection Unchanged!'
},
[PSCustomObject]@{
'Server' = 'Server3.com'
'Cube' = 'Cube3'
'Connection Details' = 'Connection changed!'
}
)
function ConvertTo-HTMLTable ($obj) {
# Accepts a System.Data.DataTable object or an array of PSObjects and converts to styled HTML table
# add type needed to replace HTML special characters into entities
Add-Type -AssemblyName System.Web
$sb = New-Object -TypeName System.Text.StringBuilder
[void]$sb.AppendLine('')
if ($null -ne $obj) {
if (([object]$obj).GetType().FullName -eq 'System.Data.DataTable'){
# it is a DataTable; convert to array of PSObjects
$obj = $obj | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors
}
$headers = $obj[0].PSObject.Properties | Select -ExpandProperty Name
[void]$sb.AppendLine('')
foreach ($column in $headers) {
[void]$sb.AppendLine(('{0} ' -f [System.Web.HttpUtility]::HtmlEncode($column)))
}
[void]$sb.AppendLine(' ')
$row = 0
$obj | ForEach-Object {
# add inline style for zebra color rows
if ($row++ -band 1) {
$tr = '' -f $oddRowBackColor
}
else {
$tr = ' '
}
[void]$sb.AppendLine($tr)
foreach ($column in $headers) {
[string]$val = $($_.$column)
if ([string]::IsNullOrWhiteSpace($val)) {
$td = ' '
}
else {
$td = '{0} ' -f [System.Web.HttpUtility]::HtmlEncode($val)
}
[void]$sb.Append($td)
}
[void]$sb.AppendLine(' ')
}
[void]$sb.AppendLine('')
}
[void]$sb.AppendLine('
')
return $sb.ToString()
}
$headerBackColor = '#4F81BD' # backgroundcolor for column headers
$oddRowBackColor = '#DCE6F1' # background color for odd rows
$style = @"
Report
"@
$body = '{0}{1}' -f $style, (ConvertTo-HTMLTable $exportObject)
Send-MailMessage -From $FromEm -Subject $Subject -To "user@domain.com" -Body $body -BodyAsHtml -SmtpServer $SmtpServer -Port $Port -Credential $Creds -UseSsl
Two examples to do table styling that mimic the MS Word Grid Table 5 Dark Accent 5
can be found here
Hope that helps