Skip to content Skip to sidebar Skip to footer

Color-code Cells In Html Mail Based On Criteria

I have a simple script which checks disk space on remote servers, it's got a simple output format which is as follows What I'd like to do, is color code the individual cells based

Solution 1:

I have written this so that it generally does what you want. It is already set to go with HTML tables and an free relay server email provider. This script takes one text input file that you can specify at the top. This text file will contain all of the server names that you want to check, on their own line. Example below. If you want to use your own email provider, you can just replace all the smtp info. Otherwise, it DOES REQUIRE admin priviledges to be run successfully. These credentials can be entered at the top of the script where $cred is specified. You will also need to change the $servers variable to point to your list of server names to check against. It currently only selects the C drive, but you can easily edit that as well as add a column for ping results.

Example of C:\temp\servers.txt

hqdc01
hqdc02
hqdc03
hqfile01
hqfile02
hqmail01
hqservices01
hqsql01
hqsql02

The .ps1 script:

$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

And last but not least, here is a picture of the sample email that you will get with this embedded table: htmltable

Post a Comment for "Color-code Cells In Html Mail Based On Criteria"