When I run my script below, extra rows get added to my HTML output and I can not figure out why. I use the same CSS style in other scripts and do not get the extra rows. I thought they might be added when running the Get-WebStatus function, but can not debug.
$style = @"
<style>
body{
font-family: Arial;
font-size: 12pt;
}
table {
border-width: 2px;
border-style: solid;
border-color: black;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
}
th {
border-width: 2px;
padding: 5px;
border-style: solid;
border-color: black;
background-color: #dddddd;
}
h2{
text-align: center;
}
h3{
text-align: center;
}
td{
border-width: 2px;
padding: 5px;
border-style: solid;
border-color: black;
}
td:nth-child(2){
font-weight: bold;
}
.pass{
background-color: #c6efce;
color: #006100;
font-weight: bold;
}
.fail{
background-color: #ffc7ce;
color: #9c0006;
font-weight: bold;
}
</style>
"@
##I then load the list of urls and applications name from a JSON file
$json = Get-Content $jsonFile -Raw | ConvertFrom-Json
Function Get-WebStatus {
param (
$site
)
$report = $site | ForEach-Object {
$URL = $_.URL
Write-Host $URL
try {
Invoke-WebRequest -UseBasicParsing -Uri $URL -TimeoutSec 60
$status = 'PASS'
}
catch {
# $StatusCode = $_.Exception.Response.StatusCode.value__
$status = 'FAIL'
}
##create table
[PSCustomObject]@{
Application = $_.Application
URL = $_.URL
Status = $status
}
}
Write-Host $report | Select-Object Application
[xml]$fullTable = $report | Select-Object Application, URL, Status | ConvertTo-Html -Fragment
##Set color for PASS & FAIL statuses
try {
$fullTable.SelectNodes("/table/tr/td[text()='PASS']").SetAttribute('class', 'pass')
$fullTable.SelectNodes("/table/tr/td[text()='FAIL']").SetAttribute('class', 'fail')
}
catch {
#No failures occured
}
Write-Output [pscustomobject] @{
'table' = $fullTable
}
}
$resultsPedme = Get-WebStatus $pedmeSites
$tablePedme = $resultsPedme.table
$resultsClaims = Get-WebStatus $claimsSites
$tableClaims = $resultsClaims.table
$body = @"
<html>
<head>
<p>
Greetings,
</p>
<p>
Automated monitoring.....
</p>
<h2>
On Demand Availability Report
</h2>
<h3>
Provider Enrollment
</h3>
$($tablePedme.InnerXml)
<h3>
Claims
</h3>
$($tableClaims.InnerXml)
</head>
</html>
"@
#Output table to reportFile
ConvertTo-Html -Head $style -Body $body | Out-File -FilePath $reportFile
##Send email functionality
$mailText = Get-Content -Path $reportFile -raw
$mailParams = @{
SmtpServer = '***'
To = '***'
From = '***'
Subject = 'testing'
Body = [string]$mailText
BodyAsHtml = $true
WarningAction = 'Ignore'
}
Send-MailMessage @mailParams
Write-Host 'Script Complete'
This is the output I receive with the extra rows.
I feel crazy not being able to figure this out…