Insert background color in a row

喜你入骨 提交于 2020-01-06 07:26:02

问题


I have a file input.txt having below content:

ENVIRONMENT   SERVER       APPLICATION     STATUS
Test          Windows      abcd            Success
Dev           Linux        wxyz            Failed
Prod          Windows      pqrs            Success
Test          Ubuntu       efgh            NoValue

And using below script I use to convert this file's content into tabular format and sending mail to people.

$smtpServer = "abc.com"
$smtpFrom = "don'treply@abc.com"
$smtpTo = "sunny@xyz.com"
$messageSubject = "bla_bla_bla"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$body = Get-Content D:\Test\input.txt | Out-String
$body = $body -replace '^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)', '<tr><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$1</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$2</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$3</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$4</th></tr>'
$body = $body -replace '\n(\S+)\s+(\S+)\s+(\S+)\s+(\S+)', '<tr><td style = "border: 1px solid black; padding: 5px;">$1</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$2</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$3</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$4</td></tr>'
$body = '<body><table style = "border: 1px solid black; border-collapse: collapse;">' + $body + '</table><br/><b>NOTE:</b> This is an automatically generated email, please do not reply to it.<br/></body>'
$message.Body = $body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

My query is it possible to modify this script in such a way that:

Row having status 'Failed' shall come in Red color in background and row having status 'NoValue' shall come in Amber color in background ?


回答1:


This slice of code is designed to be used in place of the body code of yours. This will take the text file, drop the header with -Skip 1 since that line is special. With the remaining data create a custom ps object. The object is built splitting each line on groups of whitespace. Obviously this is dependant on no whitespace in your data fields. Then it will build the html table with header row and append each row from the file individually. Applying different backround color depending on what the Status is. Most everything is stored in variables so that changes can be made globally and easily. Also makes it easier to ready. Items like $headerStyle and $normalDataStyle which are applied to <th> and <td> tags respectively. Using Switch($_.Status) we can control the attibute assigned to a particular row for bgcolor. Also on the end of every line i added `r`n. If that was not there my testing didnt format correcly and a cell would float out of table. You could remove them if need be.

# Get the file contents. Skip the first line (the header) since it will be processed seperately. 
$data = Get-Content e:\temp\input.txt | select -Skip 1 | ForEach-Object{
    # Split the row on gropus of whitespace
    $splitLine = $_ -split '\s+'
    # Organize the split into a custom object.
    [pscustomobject] @{
        'Environment' = $splitLine[0]
        'Server' = $splitLine[1]
        'Application' = $splitLine[2]
        'Status' = $splitLine[3]
    }
}

$message = "This is an automatically generated email, please do not reply to it."
$headerStyle = 'style = "border: 1px solid black; background: #dddddd; padding: 5px;"'
$normalDataStyle =  'style = "border: 1px solid black; padding: 5px;"'
$failedRowStyle = 'bgcolor="#FF0000"'
$noValueRowStyle = 'bgcolor="#FF9933"'
# Using the data object create an html formatted body for the email.
# Start the html table
$body = "<table style=`"border: 1px solid black; border-collapse: collapse;`">`r`n"
# Add the header
$body = $body + "<tr><th $headerStyle>Environment</th><th $headerStyle>Server</th><th $headerStyle>Application</th><th $headerStyle>Status</th></tr>`r`n"
# Add the lines from $data
$data | ForEach-Object{
    # Determine the row color
    Switch($_.Status){
        "Failed"{$rowStyle = $failedRowStyle}
        "NoValue"{$rowStyle = $noValueRowStyle}
        default{$rowStyle = ""}
    }
    $body = $body + "<tr $rowstyle><td $normalDataStyle>$($_.Environment)</td><td $normalDataStyle>$($_.Server)</td><td $normalDataStyle>$($_.Application)</td><td $normalDataStyle>$($_.Status)</td></tr>`r`n"
}
# End the table with the $message
$body = $body + "</table><br/><b>NOTE:</b>$message<br/>`r`n"

Here is the output from your sample

If you dont like the colours just need to change the values for $failedRowStyle and/or $noValueRowStyle




回答2:


If you remove the "| Out-String" pipe on your "Get-Content D:\Test\input.txt" line, you'll end up with an array of lines instead of one continuous string. That way you could use "foreach" to process each line separately and you could also test for the existence of "Failed" and "NoValue" at the same time and process the background accordingly.

This came out more complicated than I anticipated as I had some trouble getting the $Matches[#] variables working with the HTML formatting when I was putting them in the same lines. Adding them to the $Body by themselves seemed to solve it but bloated the code a bit.

$smtpServer = "abc.com"
$smtpFrom = "don'treply@abc.com"
$smtpTo = "sunny@xyz.com"
$messageSubject = "bla_bla_bla"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$InputText = Get-Content D:\Test\input.txt
$Body = "<body><table style = 'border: 1px solid black; border-collapse: collapse;'><tr><th style = 'border: 1px solid black; background: #dddddd; padding: 5px;'>ENVIRONMENT</th><th style = 'border: 1px solid black; background: #dddddd; padding: 5px;'>SERVER</th><th style = 'border: 1px solid black; background: #dddddd; padding: 5px;'>APPLICATION</th><th style = 'border: 1px solid black; background: #dddddd; padding: 5px;'>STATUS</th></tr>"
$InputText | Foreach {
    Clear-Variable Matches
    If ($_ -NotLike "ENVIRONMENT*") {
        If ($_ -Match '^(\S+)\s+(\S+)\s+(\S+)\s+(Failed)') {
            $Body +="<tr style = 'background: #ff0000;'><td style = 'border: 1px solid black; padding: 5px;'>"
            $Body += $Matches[1]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[2]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[3]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[4]
            $Body += "</td></tr>"
            }
        ElseIf ($_ -Match '^(\S+)\s+(\S+)\s+(\S+)\s+(NoValue)') {
            $Body +="<tr style = 'background: #ffa500;'><td style = 'border: 1px solid black; padding: 5px;'>"
            $Body += $Matches[1]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[2]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[3]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[4]
            $Body += "</td></tr>"
            }
        ElseIf ($_ -Match '^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)') {
            $Body +="<tr><td style = 'border: 1px solid black; padding: 5px;'>"
            $Body += $Matches[1]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[2]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[3]
            $Body += "</td><td style = 'border: 1px solid black; padding: 5px;text-align:center;'>"
            $Body += $Matches[4]
            $Body += "</td></tr>"
            }
        }
    }
$Body += "</table><br/><b>NOTE:</b> This is an automatically generated email, please do not reply to it.<br/></body>"
$message.Body = $Body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)


来源:https://stackoverflow.com/questions/25809637/insert-background-color-in-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!