Home > Mobile >  Powershell email sender with html body (htmlbody.Replace)
Powershell email sender with html body (htmlbody.Replace)

Time:11-25

I'm currently a C# dotnet automation engineer. One of my tests results output is via email. My tests results output goes through powershell. I'm fairly new to email templates and HTML in general. It's a simple HTML body with variables that I replace with $EmailBody= $EmailBody.Replace("PassedTests",$Passed) function etc

The whole premise: my script replaces Total tests/Passed/Failed/Skipped with data that I extract from a .trx file after the test run. My extraction code:

            $PassedTests = $testResultsXml.TestRun.ResultSummary.Counters.Passed
            $FailedTests = $testResultsXml.TestRun.ResultSummary.Counters.Failed
            $SkippedTests = $testResultsXml.TestRun.ResultSummary.Counters.Skipped
            $OnlyFailed = $testResultsXml.TestRun.Results.UnitTestResult | Where-Object { $_.outcome -eq "Failed" }
            $FailedTestsName = $OnlyFailed.TestName

I have the "Error list" table (picture below) that shows test names if there are any failed tests that in the HTML body

@</td>
</tr>
<!--end img-->

<tr>
<td height="15"></td>
</tr>

<!--title-->
<tr align="center">
<td align="center" style="font-family: 
'Open Sans', Arial, sans-serif; font-size:16px;color:#3b3b3b;font-weight: bold;">**ERROR LIST**</td>
</tr>
<!--end title-->

<tr>
<td height="10"></td>
</tr>

<!--content-->
<tr align="center">
<td align="center" style="font-family: 'Open Sans', Arial, sans-serif; font-size:12px;color:#7f8c8d;line-height: 24px;">NoErrors</td>
</tr>
<!--end content-->

</table>
</td>
</tr>
<tr>
<td height="30"></td>
</tr>
</table>

Now the main question is: is it somehow possible to ONLY show the "Error list" table only IF there are any failed tests? If there are no failed tests it would be great for that table not to be shown at all. Any kind of help would be greatly appreciated. Thanks!

$EmailBody= $EmailBody.Replace("PassedTests",$Passed)
$EmailBody= $EmailBody.Replace("FailedTests",$Failed)
$EmailBody= $EmailBody.Replace("SkippedTests",$Skipped)
$EmailBody= $EmailBody.Replace("ErrorList",$FailedTestsName)
$Emailattachment = "\TestResults.trx"

enter image description here

CodePudding user response:

You are on the good path. You just need to extend what you are doing.

  • Remove the thing that might or might not be in the email (The "errors list" section as it won't be there if there are no error)
  • Put the section your removed in its own variable
  • Add a placeholder in your main html template at the location where it is supposed to be (just like you do already so we can do a replace in the html template.

From there, the logic is :

  • If there are 0 errors, you replace the placeholder from the main template by an empty string (you don't want that placeholder to appear in the final email)
  • If there are 1 or more error, instead of replacing by your error list, you build a new variable that contain the section you want to append, then you replace its loop by the errors content and finally you replace the placeholder by that section (which contains the error loop)

That would look something like this.

$EmailBody = @'
</td>
</tr>
<!--end img-->

<tr>
<td height="15"></td>
</tr>

**ErrorsTable**

'@


$ErrorListBody = @'
<!--title-->
<tr align="center">
<td align="center" style="font-family: 
'Open Sans', Arial, sans-serif; font-size:16px;color:#3b3b3b;font-weight: bold;">**ERROR LIST**</td>
</tr>
<!--end title-->

<tr>
<td height="10"></td>
</tr>

<!--content-->
<tr align="center">
<td align="center" style="font-family: 'Open Sans', Arial, sans-serif; font-size:12px;color:#7f8c8d;line-height: 24px;">NoErrors</td>
</tr>
<!--end content-->

</table>
</td>
</tr>
<tr>
<td height="30"></td>
</tr>
</table>
'@


if ($FailedTests.Count -gt 0)  {
    # inserting errors to the `$ErrorListBody` html segment
    $ErrorsHtml = $ErrorListBody.Replace("ErrorList", $FailedTestsName)
    # inserting the html segment into the main email 
    $EmailBody = $EmailBody.Replace("**ErrorsTable**", $ErrorsHtml)
} else {
   # Removing the placeholder from the main template.
   $EmailBody = $EmailBody.Replace("**ErrorsTable**", '')
}

  • Related