创建表格并通过电子邮件发送

时间:2018-07-10 14:29:03

标签: powershell smtp

我希望能够创建一个表,然后将该表通过电子邮件发送给我自己,但在使其正常工作时遇到了很多麻烦。我相信我的桌子是正确的,但是我需要一个已经做到这一点的人来仔细检查。

$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 = "jsmith@example.com"
$From = "IT@example.com"
$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

2 个答案:

答案 0 :(得分:1)

这将很难阅读,但是我知道它就像我的手背一样,因为我在查询MS SQL和AD信息后用PS开发了HTML电子邮件。构建逻辑,然后遍历数据以构建注入所有HTML格式的HTML表

我做的所有表都使用ArrayList。要使所有功能正常工作,然后使HTML与Outlook兼容,这很复杂。不过,现在就像魅力一样。

如果您有任何疑问,并且可能正在考虑对表格使用HTML,请告诉我。

编辑:其中一些与您需要的内容无关。仅展示一些逻辑及其实现方法。在不确定公司信息的情况下不确定要包含和排除的内容。

[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>"
}

答案 1 :(得分:0)

这是一个简单得多的方法。但是,我确实感谢上一张海报的活力。

$ HTML1 =“日常健康检查应用程序 状态打印$ PrintTest Internet $ InternetTest1会议PC:$ Server1 $ ServerTest1会议PC:$ Server2 $ ServerTest2会议PC:$ Server3 $ ServerTest3

相关问题