我的文件input.txt
内容低于:
ENVIRONMENT SERVER APPLICATION STATUS
Test Windows abcd Success
Dev Linux wxyz Failed
Prod Windows pqrs Success
Test Ubuntu efgh NoValue
使用以下脚本我用来将此文件的内容转换为表格格式并向人们发送邮件。
$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)
我的查询是否可以通过以下方式修改此脚本:
Row having status 'Failed' shall come in Red color in background and row having status 'NoValue' shall come in Amber color in background ?
答案 0 :(得分:3)
这段代码旨在用来代替你的身体代码。这将采用文本文件,使用-Skip 1
删除标题,因为该行是特殊的。使用剩余数据创建自定义ps对象。该对象构建为在空白组上分割每一行。显然,这取决于数据字段中没有空格。然后它将构建带有标题行的html表,并单独追加文件中的每一行。根据状态,应用不同的背景颜色。大多数内容都存储在变量中,以便可以全局轻松地进行更改。也使它更容易准备。 $headerStyle
和$normalDataStyle
等项目分别应用于<th>
和<td>
标记。使用Switch($_.Status)
,我们可以控制分配给bgcolor
的特定行的属性。同样在每行的末尾我添加了`r`n
。如果那不存在我的测试没有正确格式化,一个单元格将浮出桌面。如果需要,你可以删除它们。
# 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"
以下是样本的输出
如果您不喜欢这些颜色,只需要更改$failedRowStyle
和/或$noValueRowStyle
答案 1 :(得分:1)
如果删除&#34; |出字符串&#34;您的&#34; Get-Content D:\ Test \ input.txt&#34;你会得到一个行数而不是一个连续的字符串。这样你就可以使用&#34; foreach&#34;分别处理每一行,你也可以测试是否存在&#34;失败&#34;和#34; NoValue&#34;同时处理背景。
这比我预想的要复杂得多,因为当我将$ Matches [#]变量用于HTML格式时,我将它们放在同一行中时遇到了一些麻烦。将它们添加到$ Body本身似乎可以解决它,但稍微膨胀了代码。
$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)