Question about ConvertTo-HTML

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Ed O'Connor Ed O’Connor 8 months ago.

  • Author
    Posts
  • #37385
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Hi am working on creating a report and just doing some preliminary testing with getting everything into a nice looking HTML report.

    Right off the bat I am running into a issue.

    $FilePath = "c:\temp\TestHTMLReport.html"
    
    $Css='table{margin:auto; width:95%}
                  Body{background-color:Green; Text-align:Center;}
                    th{background-color:black; color:white;}
                    td{background-color:Grey; color:Black; Text-align:Center;}
         '
    
    $Test1 = "This is a test!" | ConvertTo-Html -Fragment -As Table -PreContent "Test1"
    $Test2 = "This is another test!" | ConvertTo-Html -Fragment -As Table -PreContent "Test2" | 
             Out-String
    $Test3 = "This is my last test (probably not, lol)!" | ConvertTo-Html -Fragment -As Table -PreContent "Test3" | 
             Out-String
    
    $Report = ConvertTo-Html -Title "My Boring Test!" `
                             -Head "Test PowerShell ReportingFor EdThis report was ran: $(Get-Date)" `
                             -Body "$test1 $test2 $test3 $Css"
    
    $Report | Out-File $Filepath ; Invoke-Expression $FilePath
    

    My issue is the html page is not showing the text I have for the variable, just a number which seems to represent the number of characters in each variable.

    How can I get it to show the actual text? I have been doing all sorts of google searches but to no avail. The have a variable that runs a command the output shows as text with no issue.

    Thanks in advance

  • #37386
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yeah, ConvertTo-Html is a bit funny if you pipe in strings. It looks at the object for its properties (in this case, Length is the only property on a string) and makes the table out of that. You could pipe in objects with a string property instead:

    [pscustomobject]@{ Value = "This is a test!" } |
    ConvertTo-Html -Fragment -As Table -PreContent "Test1"
    
  • #37390
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Thanks this is very helpful. I tried that and it worked like I wanted. Now how can I apply that to a variable whose text is derived from an if/else statement.

                    th{background-color:black; color:white;}
                    td{background-color:Grey; color:Black; Text-align:Center;}
         '
    
    function IPv4
    {
    
    $IPv4Settings = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters" | Select-Object PSPath,SearchList,EnableLMHOSTS
    					
    $IPV4DNSSuffix = If ($IPv4Settings.SearchList -like "test1.com,ads.test.com,intranet.test.com,amdc.test.com,eudc.test.com") {write-output "IPv4 DNS Suffixes are set correctly"}
    					Else {write-output "***NON COMPLIANT*** IPv4 DNS Suffixes are not set correctly"}
    
    $IPv4Settings
    $IPv4DNSSuffix
    			
    }
    
    $test1 = ipv4 | ConvertTo-Html -Fragment -As table -PreContent "Test1" | out-string
    
    
    
    
    
    
    $Test2 = [pscustomobject]@{ Value = "This is another test!" } | ConvertTo-Html -Fragment -As Table -PreContent "Test2" | 
            Out-String
    $Test3 = [pscustomobject]@{ Value = "This is my last test (probably not, lol)!" } | ConvertTo-Html -Fragment -As Table -PreContent "Test3" | 
             Out-String
    
    $Report = ConvertTo-Html -Title "My Boring Test!" `
                             -Head "Test PowerShell ReportingFor EdThis report was ran: $(Get-Date)" `
                             -Body "$test1 $test2 $test3 $Css"
    
    $Report | Out-File $Filepath ; Invoke-Expression $FilePath
    

    When I do this I get the results for the first part of the function $IPv4Settings but nothing for $IPv4Suffix. If I type out the function at the powershell prompt it does show me the output there. I just can't get it into the HTML. Any tips/ideas?

You must be logged in to reply to this topic.