2 Replies Latest reply on Apr 1, 2014 10:29 AM by DZ1

    Converting script to HTML...I just can't seem to get it....again

    DZ1 Hot Shot



      I'm trying to convert a report to HTML, and I just can't seem to understand when I need to convert these objects.  I know the basic ConvertTo-HTML cmdlet, and I've posted up questions before and received answers, but it always turns out that something new comes up, and it's usually a different problem.  


      This is a short version of a script I’m writing, I’m trying to keep it simple for myself, so that I can understand and look at the data that is being converted.  Since my report is going to have the VMname, and OS, I want an HTML document with the table columns  as VMname, and OS, and then the row will have the data right below the column.  I’ve looked online at many examples online, but I can’t seem to find something for my situation.  I’ve seen the use of ConvertTo-HTML -fragment, and other examples, but it does not work out the way I want. 


      How would the script below produce what I’m asking for?  If I can get these 2 items, then I feel that I can use that example and finish the rest of the script. 


      Thanks in advance.


      $custom = "<style>


      body {background-color: lightblue; }


      table {background-color: white; margin: 5px; float: left; top: 0px; display: inline-block; padding: 5px; border: 1px solid black }


      tr:nth-child(odd) {background-color: lightgray}


      </style>"


      $AllReport = $null


      $AllReport = @()


      Get-Folder Folder | Get-VM | foreach {


      $VMlist = $_


      $Report = "" | Select VMname, OS


      $Report.VMname = $VMlist.name


      $Report.OS = $VMlist.guest.OSFullname


      $AllReport += $Report


      #$AllReport | ConvertTo-Html -As Table -Head $custom 


      #$AllReport  | Out-File c:\Output\HTMLout.htm


      }


      #$HTML | Out-File c:\output\testhtm.html


      #ii c:\Output\testhtm.html

        • 1. Re: Converting script to HTML...I just can't seem to get it....again
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Try like this

           

          $custom = @"
          <style>
          body {background-color: lightblue; }
          table {background-color: white; margin: 5px; float: left; top: 0px; display: inline-block; padding: 5px; border: 1px solid black }
          tr:nth-child(odd) {background-color: lightgray}
          </style>
          "@


          $AllReport = $null
          $AllReport = @()
          Get-Folder Folder | Get-VM | foreach {
           
          $VMlist = $_
           
          $Report = "" | Select VMname, OS
           
          $Report.VMname = $VMlist.name
           
          $Report.OS = $VMlist.guest.OSFullname
           
          $AllReport += $Report
          }
          $AllReport | ConvertTo-Html -As Table -Head $custom |
          Out-File htmltest.htm
          Invoke-Item htmltest.htm

           

          The basic concept for producing HTML output is in essence quite simple:

          • Define one or more styles ($custom) you want to use in the HTML output. I prefer to use here-strings to do this
          • Collect the data you want to display in an array ($AllReport)
          • Run the data in the array through the ConvertTo-Html cmdlet, using the style(s) you defined before
          • Write the produced HTML code to a file
          • Optionally, show the HTML file
          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Converting script to HTML...I just can't seem to get it....again
            DZ1 Hot Shot



            Like always, thanks a million.  It's always much easier than I think it is.  I was doing way too much.  Everything is working, thanks again, and I can't wait for the new PowerCLI book from you...good luck with that.