48075

Insert background color in a row

Question:

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 ?

Answer1:

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

<img alt="Sample" class="b-lazy" data-src="https://i.stack.imgur.com/5UNQi.jpg" data-original="https://i.stack.imgur.com/5UNQi.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

Answer2:

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)

Recommend

  • zend smtp mail crashes after 100+ mails
  • Image in Email Subject using PHP
  • How to configure Monolog to send errors via SwiftMailer to alternate mailer in Symfony2
  • regex, multiline extract in R
  • How do one only submit click event (div), when using jquery live function?
  • PHP - sending email with attachment does not show message content
  • How to reply a tweet using the Twitter gem?
  • TCPDF's getNumLines() is sometimes wrong
  • dc.js: Reducing rows in data table
  • get latest text entry of GROUP BY statement
  • Zend Framework 2, Module Redirect
  • Play Framework nested form errors missing
  • Why does PHP appear to evaluate this condition incorrectly?
  • how to convert open xml representation of word document into RTF format using c#.net 4.0?
  • Extract data between rows r
  • SmtpClient sends email to junk
  • Contact form problem - I do receive messages, but no contents (blank page)
  • CSS: How to fix overlapping divs
  • IOS > Open URL in Safari with POST
  • Failed to find version-tag string. File must be updated
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Plotting densities in R
  • Update Google Maps traffic layer without page reloading
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Installing iPhone App to iPhone
  • Play WS (2.2.1): post/put large request
  • swift auto completion not working in Xcode6-Beta
  • Excel - Autoshape get it's name from cell (value)
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Delete MySQLi record without showing the id in the URL
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Unanticipated behavior
  • Comma separated Values
  • JTable with a ScrollPane misbehaving
  • Trying to get generic when generic is not available
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to Embed XSL into XML
  • To Get the radio button value in ruby on rails