JRN
JRN

Reputation: 279

Create and e-mail a table

I would like to be able to create a table and then e-mail that table to myself and I am having a lot of trouble making it work. I believe that my table is correct but I need someone who has done this before to look over this.

$PrintTest = "PASSED"
$InternetTest1 = "PASSED"
$InternetTest2 = "PASSED"
$SkypeServiceStatus = "PASSED"
$SharePointServiceStatus = "PASSED"

$table = New-Object system.Data.DataTable “Daily Morning Health Checks”

$col0 = New-Object system.Data.DataColumn TEST,([string])

$col1 = "Printing"
$col2 = "Google"
$col3 = "MSN"
$col4 = "Skype"
$col5 = "Intranet"

$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)

$row0 = $table.NewRow();$row = "TEST and RESULTS";
$row1 = $table.NewRow();$row = $PrintTest;$row = $InternetTest1;$row = $InternetTest2;$row = $SkypeServiceStatus;$row = $SharePointServiceStatus
$table| Format-Table -AutoSize


<#****Test E-Mail****#>
$To = "[email protected]"
$From = "[email protected]"
$Subject = "Daily Health Check"
$SMTPServer = "MY_Server"
$Body = $Table
$Port = "25"

Send-MailMessage -To $To -Cc $CarbonCopy -From $From -Subject $Subject -SmtpServer $SMTPServer -Body $Body -Port $Port

Upvotes: 0

Views: 142

Answers (2)

JRN
JRN

Reputation: 279

This is a much simpler method. I do however appreciate the vigor of the previous poster.

$HTML1 = " Daily Health Checks ApplicationStatus Printing$PrintTest Internet$InternetTest1 Conference PC: $Server1 $ServerTest1 Conference PC: $Server2$ServerTest2 Conference PC: $Server3$ServerTest3

Upvotes: 0

Clayton Lewis
Clayton Lewis

Reputation: 394

This is going to be difficult to read but I know it like the back of my hand because I had developed an HTML email with PS after querying MS SQL and AD for info. Built the logic and then looped over the data to build the HTML Tables injecting all the HTML formatting

All tables I did were using an ArrayList. It was complicated to get everything to work and then have the HTML work with Outlook. Works like a charm now though.

Please let me know if you have a question and might be considering using HTML for the tables.

EDIT: Some of this won't pertain to what you need. Just showing some of the logic and how it can be done. Wasn't sure what to include and exclude without revealing company info.

[string]$HTML_Head = "
<head>
    <style>
        th.h1 {font-size: 20px; background-color: #709faf; font-family: Arial Bold, Arial, sans-serif; font-weight: bold; text-align: center; border: solid; border-width: 1px}
        th.h2 {font-size: 15px; font-family: Arial Bold, Arial, sans-serif; font-weight: bold; text-align: center}
        th.s1 {font-size: 13px; background-color: #f0f5f7; font-family: Arial Bold, Arial, sans-serif; font-weight: bold; padding: 2px 4px 2px 4px}
        td.s1 {vertical-align: top}
        td.s2v1 {font-size: 12px; background-color: #a9c5cf; font-family: Arial Bold, Arial, sans-serif; font-weight: bold; text-align: center; padding: 2px 4px 2px 4px}
        td.s2v2 {font-size: 12px; background-color: #d9e8ee; font-family: Arial Bold, Arial, sans-serif; font-weight: bold; text-align: center; padding: 2px 4px 2px 4px}
        table.s1 {font-size: 12px; background-color: #b7cfd7; border: solid; border-width: 2px}
        table.s2 {font-size: 12px; background-color: #709faf; border: solid; border-width: 1px; width: 100%}
    </style>
</head>"

[string]$HTML_Body = "
<body>
    <table class='s1'>
        <tr>
            <th class='h1' colspan='3'>Table Name</th>
        </tr>
        <tr>
            <td class='s1'>
                $Table_1
            </td>
            <td class='s1'>
                $Table_2
            <br>
                $Table_3
            </td>
            <td class='s1'>
                $Table_4
            </td> 
        </tr>
    </table>
</body>"

Function Create-Table([string]$Input_Title, [string]$Input_Colspan, [System.Collections.ArrayList]$Input_Headers, `
        [System.Collections.ArrayList]$Input_Data, [string]$Input_Primary_Condition, [string]$Input_Secondary_Condition, [int]$Input_Logic_1, [string]$Input_Logic_2, `
        [string]$Input_Change_Type, [string]$Input_Change_Logic) {

    [string]$Table_Colspan = "'" + $Input_Colspan + "'"
    [string]$Title = $Input_Title
    [string]$Condition_1 = $Input_Primary_Condition
    [string]$Condition_2 = $Input_Secondary_Condition
    [int]$Logic_1 = $Input_Logic_1
    [string]$Logic_2 = $Input_Logic_2
    [System.Collections.ArrayList]$Headers = $Input_Headers
    [string]$Table_Style_Counter = "1"
    [string]$Table_Title = "
        <tr class='s1'>
            <th class='h2' colspan=$Table_Colspan>$Title</th>
        </tr>"
    [string]$Table_Header = "`n<tr>" + ($Headers | ForEach-Object {"`n<th class='s1'>$_</th>"}) + "`n</tr>"

    If ($Input_Data) {
        [string]$Table_Data = $Input_Data | ForEach-Object {
            If ($Table_Style_Counter -eq 1) {
                $Table_Style_Version = "'s2v1'"
                $Table_Style_Counter = 2
            }
            Else {
                $Table_Style_Version = "'s2v2'"
                $Table_Style_Counter = 1
            }

            $ID = $_

            "`n<tr class='s1'>" 
            $Headers | ForEach-Object {
                $Line_Data = "`n<td class=$Table_Style_Version>" + $ID.$_ + "</td>"

                If ($Input_Change_Type -eq "1") {            
                    If ($_ -eq $Condition_1) {
                        $Line_Data = $Line_Data -replace "'>", "' style='background-color: #f4a460'>"
                    }
                    Elseif ($_ -eq $Condition_2) {
                        $Line_Data = $Line_Data -replace "'>", "' style='background-color: #ffff99'>"
                    }                
                    Else {
                        $Line_Data = $Line_Data
                    }

                    If ($_ -eq "Days") {
                        If (($ID.Days -as [int]) -ge 20) {
                            $Line_Data -replace "'>", "' style='background-color: #e74c3c'>"
                        }
                        ElseIf (($ID.Days -as [int]) -ge 10) {
                            $Line_Data -replace "'>", "' style='background-color: #f1948a'>"
                        }
                        Else {
                            $Line_Data
                        }
                    }
                    Else {
                        $Line_Data
                    }
                }
                ElseIf ($Input_Change_Type -eq "2") {
                    If ($_ -eq $Logic_2) {
                        If ($ID.$_ -gt $Logic_1) {
                            $Line_Data = $Line_Data -replace "'>", "' style='background-color: #f4a460'>"
                        }
                        Else {
                            $Line_Data = $Line_Data
                        }
                    }
                    Else {
                        $Line_Data = $Line_Data
                    }

                    If ($_ -eq "Change") {                    
                        If (($ID.Change -as [int]) -gt 0) {
                            $Line_Data -replace "'>", "' style='background-color: #f4a460'>"
                        }
                        ElseIf (($ID.Change -as [int]) -lt 0) {
                            $Line_Data -replace "'>", "' style='background-color: #98fb98'>"
                        }
                        Else {
                            $Line_Data
                        }
                    }
                    Else {
                        $Line_Data
                    }
                }
            }

            "`n</tr>"
        }
    }
    else {
        [string]$Column_Count = "'" + $Headers.Count + "'"
        [string]$Table_Data = "<td class='s2v1' style='background-color: #98fb98' colspan=$Column_Count>No Errors</td>"
    }

    Return "<table class='s2'>
                $Table_Title
                $Table_Header
                $Table_Data
            </table>"
}

Upvotes: 1

Related Questions