Need Help Please with a report

Jason L 41 Reputation points
2021-07-07T19:17:54.153+00:00

I am running the below and it sends me an email that is table formated with columes Name and PSComputername. What I am trying to figure out is how can I make the table show red when the domain profile is not equal to DomainAuthenticated.

$time=[datetime]::Today.AddDays(-30)
$Servers = Get-ADComputer -Filter {LastLogonDate -gt $time -And OperatingSystem -Like 'server' } -SearchBase "OU=Domain,DC=com"
$Results = ForEach ($Server IN $Servers){
Invoke-Command -computername $Server.Name -ScriptBlock {Get-NetConnectionProfile} | Select Name,PSComputername
}
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

$HTML = $Results | ConvertTo-Html -Head $Header -PreContent "<h3>Firewall Profile Status</h3>" -PostContent "<p><br/><h6>Run on: $(Get-Date)</p>" | Out-String
Send-MailMessage -From 'Status@keyman .com' -To 'user@keyman .com -Subject 'Firewall Profile Status' -SMTPServer "mail.domain.com" -Body $html -BodyAsHtml

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,530 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,401 Reputation points
    2021-07-07T19:58:49.663+00:00

    To do this in HTML you'll need to apply styling (or preferably CSS) to the row or cell. That isn't possible with ConvertTo-Html. It does not provide any per-row/cell formatting support. You can apply formatting to the entire table of course but that isn't sufficient.

    The workaround that I've seen used is to do post-processing on the HTML to add in the styling. This can be easy or hard to do depending upon the data. For simple cases you can do string replacement. For example if you want a cell to be a different color when it has a certain value then a simple .Replace("<td>someValue</td>", "<td class=\"styleMe\">someValue</td>") and include the CSS class as part of your pre content. If the rules are more complex then you have more work to do. For example if you wanted the entire row a different color then you'd need to parse the HTML string directly (perhaps using -Fragment to get just the HTML table), find the cell(s) you care about and then backtrack to the find the <tr> that marks the containing row. Not terribly difficult to do but would require a good set of helper functions and testing to get right.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,796 Reputation points
    2021-07-08T15:29:37.06+00:00

    I think you're going to have to forgo the use of the ConvertTo-HTML cmdlet and construct the page yourself. Using CSS is one way to accomplish the task, but you can pretty easily construct the report yourself using PowerShell. Here's an example that should work for the report you're trying to create (note that it uses the "bgcolor" for a table cell and that's been deprecated, although it's still pretty widely supported by browsers).

    $DateRun = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
    $time=[datetime]::Today.AddDays(-30)
    Get-ADComputer -Filter {LastLogonDate -gt $time -And OperatingSystem -Like 'server' } -SearchBase "OU=Domain,DC=com" |
        ForEach-Object{
            $tablerows = @()
            Invoke-Command -computername $_ -ScriptBlock {Get-NetConnectionProfile} -Credential $cred |
                ForEach-Object{
                    if ($_.NetworkCategory -ne "DomainAuthenticated"){
                        $bgcolor = "FF0000"
                    }
                    Else {
                        $bgcolor = "#FFFFFF"    # White
                    }
                    $tablerows += @"
    <tr>
            <td>$($_.NetworkCategory)</td>
            <td bgcolor=$($bgcolor)">$($_.PSComputerName)</td>
    </tr>
    "@
                }
        }
    
    $HTMLstuff = @"
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <style>
            TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
            TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
            TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
          </style>
        </head>
        <body>
          <h3>Firewall Profile Status</h3>
          <table>
            <colgroup>
              <col/>
              <col/>
            </colgroup>
            <tr>
              <th>NetworkCategory</th>
              <th>PSComputerName</th>
            </tr>
            $tablerows
          </table>
          <p>
            <br/>
            <h6>Run on: $($DateRun)</h6>
          </p>
        </body>
        </html>
    "@
    
    $HTMLstuff | Out-File c:\junk\x.html
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.