HTML邮件中的颜色代码单元格基于标准

时间:2017-01-05 03:10:09

标签: html email powershell

我有一个简单的脚本来检查远程服务器上的磁盘空间,它有一个简单的输出格式,如下所示

enter image description here

我想做的是根据各个单元格对各个单元格进行颜色编码。具体来说,如果任何驱动器上的可用空间小于300,我想让单元格为红色。根据PowerShell Send-MailMessage以HTML格式发送的结果,最简单的方法是什么?

以下是我从远程服务器收集数据的代码:

function Set-AlternatingRows {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]$Line,

        [Parameter(Mandatory)]
        [string]$CSSEvenClass,

        [Parameter(Mandatory)]
        [string]$CSSOddClass
    )
    Begin {
        $ClassName = $CSSEvenClass
    }
    Process {
        if ($Line.Contains("<tr><td>")) {
            $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
            if ($ClassName -eq $CSSEvenClass) {
                $ClassName = $CSSOddClass
            } else {
                $ClassName = $CSSEvenClass
            }
        }
        return $Line
    }
}

foreach ($Item in $InputCSV) {
    Write-Host "Checking $($Item.Server) now"
    $ShortDate = (Get-Date).ToString('MM/dd/yyyy')

    $Ping = PingEM $Item.Server

    ### Check if server is online before proceeding
    if ($Ping -eq "Online") {
        Write-Host "$($Item.Server) is Online" -ForegroundColor Green

        $FreeSpaceAll = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -Computer $($Item.Server) |
                        Select SystemName, DeviceID, VolumeName,
                            @{Name="Total Size (GB)";E={"{0:N1}" -f($_.Size/1gb)}},
                            @{Name="Free Space (GB)";E={"{0:N1}" -f($_.Freespace/1gb)}}
        $FreeSpaceC = $FreeSpaceAll | Where {$_.DeviceID -eq "C:"}  | Select -expand "Free Space (GB)"
        $FreeSpaceD = $FreeSpaceAll | Where {$_.DeviceID -eq "D:"}  | Select -expand "Free Space (GB)"
        $FreeSpaceE = $FreeSpaceAll | Where {$_.DeviceID -eq "E:"}  | Select -expand "Free Space (GB)"
    } # Ping

    $outarray += New-Object PsObject -Property @{
        Server = $Item.Server
        FreeSpaceAll = $FreeSpaceAll
        FreeSpaceC = $FreeSpaceC
        FreeSpaceD = $FreeSpaceD
        FreeSpaceE = $FreeSpaceE
        PingResults = $Ping
    } # OutArray New-Object
} # For

$Head = Get-Content "$Dir\CSS.txt"
$Pre = "This email contains info on C/D/E free space on remote servers<br>    <br>" 
$Pre += "The below report data is available in Excel .CSV format $Link1.     After 7 days, the reports will be moved $Link2 <br><br>"

$Output = $OutArray |
          Select Server, PingResults, @{E={$_.FreeSpaceC};Label="Free Space C: (GB)"},
              @{E={$_.FreeSpaceD};Label="Free Space D: (GB)"},
              @{E={$_.FreeSpaceE};Label="Free Space E: (GB)"} |
    ConvertTo-Html -Head $Head -PreContent $Pre -As Table | Set-AlternatingRows -CSSOddClass odd -CSSEvenClass even |
    Out-String

$Subject = "($ScriptVer) - file server C/D/E free space report"
$TSBody = ""
$TSBody += "<font face ='arial' color='black'>$Output </font><br><br>"

Send-NailMessage $To -Subject $Subject -Body $TSbody -BodyAsHtml -From $From -SmtpServer $Mailer

1 个答案:

答案 0 :(得分:2)

我写了这个,所以它通常做你想要的。它已经设置为与HTML表和免费中继服务器电子邮件提供商一起使用。此脚本采用一个您可以在顶部指定的文本输入文件。此文本文件将包含您要在其自己的行上检查的所有服务器名称。以下示例。如果您想使用自己的电子邮件提供商,则可以替换所有smtp信息。否则,它需要管理员权限才能成功运行。可以在指定$cred的脚本顶部输入这些凭据。您还需要更改$servers变量以指向要检查的服务器名称列表。它目前只选择C驱动器,但您可以轻松编辑它,也可以为ping结果添加一列。

C:\ temp \ servers.txt

的示例
hqdc01
hqdc02
hqdc03
hqfile01
hqfile02
hqmail01
hqservices01
hqsql01
hqsql02

.ps1脚本:

    $cred = Get-Credential -Credential 'domain\domainadminuser'
    $ServerName = Get-Content "C:\temp\servers.txt"
    $ConvertToGB = (1024 * 1024 * 1024)
    $enter1 = "`r"
    $enter2 = "`r`n"

    # Smtp deets
    $smtpServer = "relay.appriver.com"
    $smtpPort = "2525"
    $smtpFrom = "from@from.com"
    $smtpTo = "myself@myself.com"
    $messageSubject = "Daily Server Report"

    # Set up an SmtpClient
    $smtpClient = New-Object Net.Mail.SmtpClient
    $smtpClient.Host = $smtpServer
    $smtpClient.Port = $smtpPort

    # Create the MailMessage 
    $mailMessage = New-Object Net.Mail.MailMessage
    $mailMessage.From = $smtpFrom
    $mailMessage.To.Add($smtpTo)
    $mailMessage.Subject = $messageSubject
    $mailMessage.IsBodyHtml = $true

    # style
    $htmlReport += "<style>"
    $htmlReport += "BODY{background-color:white;}"
    $htmlReport += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $htmlReport += "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
    $htmlReport += "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
    $htmlReport += "</style>"


    # table
$htmlReport += "<table>"
$htmlReport += "`n"
$htmlReport += "<tr>"
$htmlReport += "<th>ServerName</th>"
$htmlReport += "<th>Total C:</th>"
$htmlReport += "<th>Free C:</th>"
$htmlReport += "<th>% Free C:</th>"
$htmlReport += "<th>Total D:</th>"
$htmlReport += "<th>Free D:</th>"
$htmlReport += "<th>% Free D:</th>"
$htmlReport += "<th>Total E:</th>"
$htmlReport += "<th>Free E:</th>"
$htmlReport += "<th>% Free E:</th>"
$htmlReport += "</tr>"
foreach($Server in $ServerName)
{
    #Get info on all C: drives
    $diskC = Get-WmiObject -Credential $cred Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
    $htmlReport += "<tr>"
    $htmlReport += "<td>$($Server)</td>"
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskC.Size / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskC.FreeSpace / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }   
    try
    {
        if([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100) -le 10)
        {
            $htmlReport += "<td><font color=red> $([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100) -gt 10 -and [Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100) -le 20)
        {
            $htmlReport += "<td><font color=orange> $([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100) -gt 20)
        {
            $htmlReport += "<td><font color=green> $([Math]::Truncate(($diskC.FreeSpace / $diskC.size) * 100))  % </font></td>"
        }
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }   

    #Get info on all D: drives
    $diskD = Get-WmiObject -Credential $cred Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='D:'" | Select-Object Size,FreeSpace
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskD.Size / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskD.FreeSpace / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    try
    {
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -le 10)
        {
            $htmlReport += "<td><font color=red> $([Math]::Truncate(($diskD.FreeSpace / $diskD.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -gt 10 -and [Math]::Truncate(($diskD.FreeSpace / $diskD.size) * 100) -le 20)
        {
            $htmlReport += "<td><font color=orange> $([Math]::Truncate(($diskD.FreeSpace / $diskD.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -gt 20)
        {
            $htmlReport += "<td><font color=green> $([Math]::Truncate(($diskD.FreeSpace / $diskD.size) * 100))  % </font></td>"
        }
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }

    #Get info on all E: drives
    $diskE = Get-WmiObject -Credential $cred Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='E:'" | Select-Object Size,FreeSpace
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskE.Size / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    try
    {
        $htmlReport += "<td>$([Math]::Truncate($diskE.FreeSpace / $ConvertToGB))  GB </td>"
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    try
    {
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -le 10)
        {
            $htmlReport += "<td><font color=red> $([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -gt 10 -and [Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -le 20)
        {
            $htmlReport += "<td><font color=orange> $([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100))  % </font></td>"
        }
        if([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100) -gt 20)
        {
            $htmlReport += "<td><font color=green> $([Math]::Truncate(($diskE.FreeSpace / $diskE.size) * 100))  % </font></td>"
        }
    }
    catch
    {
        $htmlReport += "<td>NA</td>"
    }
    $htmlReport += "</tr>"
}

$htmlReport += "</table>"



# Now create an AlternateView from the HTML contents
$messageBody = [Net.Mail.AlternateView]::CreateAlternateViewFromString($htmlReport, 'text/html')

# Add the HTML view to the MailMessage
$mailMessage.AlternateViews.Add($messageBody)

# And finally send the message
$smtpClient.Send($mailMessage)
pause

最后但并非最不重要的是,这是您将使用此嵌入式表格获得的示例电子邮件的图片: htmltable