2 Replies Latest reply on Dec 11, 2013 8:50 AM by DZ1

    Converting my report to HTML

    DZ1 Hot Shot

      I am in the process of trying to make my email report into a nice looking html file.  I'm familiar with the basics of ConvertTo-html, and I have used it with short snapshot reports.  My problem is that now I want to take a long report that contains a lot of PSObjects, and I'm running into a problem.  I've looked online for help, and my basic idea is that I need to use the ConvertTo-HTML cmdlet with the -Fragment parameter, because I'm going to have different objects in it.  Here is a sample of what I tried.  I'm also attaching 2 files, htmlsample and htm3 which is the output from each section.  Sorry if this is very long.  Here is the first thing I tried, this is a portion of the script, if I can get 1 section working, I should be able to handle the rest:

       

      $custom = '<style>

      BODY{background-color:#999966;}

      TABLE{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

      TH{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      TD{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      </style>'

       

      $AllDataCenter = Get-Datacenter | Select -ExpandProperty Name

      $AllCluster = Get-Cluster | Sort

      $AllESXiHost = Get-VMHost

      $AllTemplate = Get-Template

       

      #Creating a new Powershell Object to store information (for ESXi Hosts)

      $ESXiInfo = New-Object -TypeName PSObject

      $vVersions = $AllESXiHost | Group Version | Select Name, Count

      $HWModel = $AllESXiHost | Group Model | Select Name, Count

      $ESXiInfo | Add-Member -MemberType NoteProperty -Name "Total ESXi hosts" -Value ($AllESXiHost).count

      $vVersions | Foreach { Add-Member -InputObject $ESXiInfo -MemberType NoteProperty -Name "vSphere Version: $($_.Name)" -Value $_.Count }

      $HWModel | Foreach { Add-Member -InputObject $ESXiInfo -MemberType NoteProperty -Name "HW Model: $($_.Name)" -Value $_.Count }

       

      $ESXiInfo | ConvertTo-Html -Fragment

       

      #Creating a new Powershell Object to store information (for Datacenter)

      $vDataCenter = New-Object -TypeName PSObject

      $vDataCenter | Add-Member -MemberType NoteProperty -Name "vSphere Datacenter(s)" -Value ([string]::Join(", ",$ALLDataCenter))

       

      $vDataCenter | ConvertTo-Html -Fragment

       

      ConvertTo-Html -Body "$ESXiInfo $vDatacenter" -Head $custom | Out-File c:\Scripts\htmlsample.htm

       

      See htmlsample.png

       

      I tried a shorter version where I used the variable $ESXiInfo, I only wrote it out to my screen and it looks like this, which is the look I'm going for:

       

      Total ESXi hosts               : 29

      vSphere Version: 5.0.0         : 27

      vSphere Version: 4.1.0         : 2

      HW Model: ProLiant BL460c G6   : 4

      HW Model: ProLiant BL490c G6   : 9

      HW Model: ProLiant BL460c Gen8 : 3

      HW Model: ProLiant BL460c G1   : 9

      HW Model: ProLiant BL460c G7   : 2

      HW Model: ProLiant DL160 G6    : 2

       

      If I try this, the html fine is kind of OK, but this is only 1 object, and I want the data in a list format.  See htm2.png

       

      $custom = '<style>

      BODY{background-color:#999966;}

      TABLE{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

      TH{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      TD{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      </style>'

       

      $ESXiInfo |  ConvertTo-Html -Head $custom | Out-File c:\Scripts\htm2.htm

       

      Even if I try this, the data does not format, but I guess that's because I'm converting it to HTML

       

      $custom = '<style>

      BODY{background-color:#999966;}

      TABLE{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

      TH{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      TD{border-width:1px; padding:0px; border-style:solid; border-color:black;}

      </style>'

       

       

       

      $ESXiInfo | ConvertTo-Html -Head $custom | fl | Out-File c:\Scripts\htm4.htm  No Attachment, it looks the same as htm2.png

       

      I'm using Powershell 4 and PowerCLI 5.5. 

       

      The basics of what I want is to put data on each row, somewhat similar to the htm2.png, but formatted like a list, and then I need to combine multiple PSObjects into one html file.  Thanks for any help. 

        • 1. Re: Converting my report to HTML
          Zsoldier Expert
          vExpert

          The main issue here is in the way you've formatted your dataset.  In other words, your defining headers for each piece of data when you need to be doing is defining two headers "Info, Count".

           

          For each of your unique values, you should be populating Info and Count, like so:

           

          $ESXiInfo = @()

          $vVersions = $AllESXiHost | Group Version | Select Name, Count

          $HWModel = $AllESXiHost | Group Model | Select Name, Count

           

           

          $DataPoint = New-Object system.Object

          $DataPoint | Add-Member -MemberType NoteProperty -Name "Info" -Value "Total ESXi hosts"

          $DataPoint | Add-Member -MemberType NoteProperty -Name "Count" -Value ($AllESXiHost).count

          $ESXiInfo += $DataPoint

           

           

          $vVersions | Foreach {

          $DataPoint = New-Object -TypeName system.Object

          $DataPoint | Add-Member -MemberType NoteProperty -Name "Info" -Value "vSphere Version: $($_.Name)"

          $DataPoint | Add-Member -MemberType NoteProperty -Name "Count" -Value $_.Count

          $ESXiInfo += $DataPoint

          }

           

           

          $HWModel | Foreach {

          $DataPoint = New-Object -TypeName system.Object

          $DataPoint | Add-Member  -MemberType NoteProperty -Name "Info" -Value "HW Model: $($_.Name)" 

          $DataPoint | Add-Member  -MemberType NoteProperty -Name "Count" -Value $_.Count

          $ESXiInfo += $DataPoint

          }

           

          In this case, we're defining $ESXiInfo as the main hash table and populating it w/ new datapoint objects.

          • 2. Re: Converting my report to HTML
            DZ1 Hot Shot

            Thanks for your help, I really appreciate it.