10 Replies Latest reply on Mar 17, 2011 2:31 AM by avlieshout

    HTML report

    jingleharish Enthusiast

      Assistance required in the below code with the formatting in HTML report with the color coding like this below.

       

      If  the created date is older than 3 days the row should be red color,  similarly if older than 2 days Yellow & 1 day or more with Green  color.

       

      Also, is it possible to include snapshot size & description in the report.

       

      #############################################################################################

      $stamp = get-date -f ddMMMyy-hhmmss
      Connect-VIServer $vcservers -Credential $cred
         
      $before = @()
      $SnapReport = "C:\Daily_Reports\Snapshots\Reports\Snapshots-Report-$stamp.html"
         
      #get vm views for all vms with snap
      Get-View -ViewType virtualmachine | where { $_.snapshot -ne $null } | foreach {

       

          $snaptree += $_.snapshot.rootsnapshotlist
           $before = $snaptree | select @{"n"="VMName";  "e"={$($(get-viobjectbyViView -moref $_.vm).name)}}, @{"n"="SnapName";  "e"={$_.name}}, @{"n"="Created"; "e"={$_.createtime}}       
      }
         
      $a = "<style>"
      $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
      $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
      $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
      $a = $a + "</style>"

       

      $header = "<H1>Pending VM Snapshots :: $stamp</H1>"
      $title = "Vmware Snapshots Reports"  
             
      $before | ConvertTo-Html -head $a -body $header -title $title | Out-File $SnapReport

      #################################################################################################

        • 1. Re: HTML report
          Zsoldier Expert
          vExpert

          I did something similar in my Summary Report.

          https://docs.google.com/View?id=ddvthst9_17hc7vkhd9

           

          Specifically, you need to do something like this:

           

          $NewObject = @()
          Foreach ($Snap in $SnapTree)
          {
          $TempObj = "" | Select VMName, SnapName, CreateTime
          $TempObj.VMName = $Snap.Name
          $TempObj.SnapName = $Snap.SnapName
          If ($Snap.CreateTime -lt ((Get-Date).AddDays(-3))){$TempObj.CreateTime = "<p style='font-weight:bold;color:red'>$($Snap.CreateTime)%</p>"}
          Else{$TempObj.CreateTime = $Snap.CreateTime}
          $NewObject += $TempObj
          }
          

           

          It's probably not exact, but you can take a look @ my summary script for snippets on how I did it.

          • 2. Re: HTML report
            LucD Guru
            vExpertUser ModeratorsCommunity Warriors

            I took the method Chris gave for colouring the output and added the size and description columns.

            Have a look

             

            #############################################################################################  $stamp = get-date -f ddMMMyy-hhmmss # Connect-VIServer $vcservers -Credential $cred    
            $before = @()
            $SnapReport = "C:\Snapshots-Report-$stamp.html"   
            #get vm views for all vms with snap
            Get-View
            -ViewType virtualmachine | where { $_.snapshot -ne $null } | foreach {     $snaptree += $_.snapshot.rootsnapshotlist }    $a = "<style>"
            $a
            = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
            $a
            = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
            $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
            $a
            = $a + "</style>"
            $header = "<H1>Pending VM Snapshots :: $stamp</H1>"
            $title
            = "Vmware Snapshots Reports"         $now = Get-Date$before = $snaptree | %{     $vm = get-viobjectbyViView -moref $_.vm     $age = ($now - $_.createtime).Days     if($age -gt 3){         $code = "red"    }     elseif($age -gt 2){         $code = "yellow"    }     else{         $code = "green"    }     $dateHtml =     '<p style=''color:' + $code + ';''>' + $_.CreateTime + '</p>'     $_ | Select @{N="VMName";E={$vm.name}},                 @{N="SnapName";E={$_.name}},                 @{N="Created";E={$dateHtml}},                 @{N="Description";E={$_.Description}},                 @{N="SizeMB";E={(Get-Snapshot -Name $_.Name -VM $vm).SizeMB}} }                $before | ConvertTo-Html -head $a -body $header -title $title | `
            foreach {$_.replace("&lt;","<").replace("&gt;",">")} | Out-File $SnapReport #################################################################################################
            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
            • 3. Re: HTML report
              LucD Guru
              Community WarriorsvExpertUser Moderators

              It looks as if one of the guests was in a funny state (deleted or not completely created) when you ran the script.

              You could try to find out which guest with these lines. Perhaps try something like this

               

              Get-View -ViewType VirtualMachine | ` 
              Select
              VMName,@{N="Error";E={$Error.Clear();Get-ViObjectByVIView -MoRef $_.MoRef -ea SilentlyContinue | Out-Null; $Error[0].Exception}}
              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              1 person found this helpful
              • 4. Re: HTML report
                jingleharish Enthusiast

                Thanks Luc, That one worked,

                 

                Need some more modifications, Need to add a new row at the bottom indicating the total size of all the snapshots & also export the same report to csv file as well.

                • 5. Re: HTML report
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  Try this

                   

                  #############################################################################################   
                  $stamp = get-date -f ddMMMyy-hhmmss
                  Connect-VIServer $vcservers -Credential $cred   
                  $before = @()
                  $snaptree = @()
                  $SnapReport = "C:\Snapshots-Report-$stamp.html"  
                  #get vm views for all vms with snap
                  Get-View -ViewType virtualmachine | where { $_.snapshot -ne $null } | foreach {
                     
                  $snaptree += $_.snapshot.rootsnapshotlist
                  }
                    
                  $a = "<style>"
                  $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
                  $a
                  = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
                  $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
                  $a = $a + "</style>"
                  $header = "<H1>Pending VM Snapshots :: $stamp</H1>"
                  $title
                  = "Vmware Snapshots Reports" 
                        
                  $now = Get-Date
                  $before
                  = $snaptree | %{
                     
                  $vm = get-viobjectbyViView -moref $_.vm
                     
                  $age = ($now - $_.createtime).Days
                     
                  if($age -gt 3){
                         
                  $code = "red"    }
                     
                  elseif($age -gt 2){
                         
                  $code = "yellow"    }
                     
                  else{
                         
                  $code = "green"    }
                     
                  $dateHtml =     '<p style=''color:' + $code + ';''>' + $_.CreateTime + '</p>'
                      $_ | Select @{N="VMName";E={$vm.name}},
                                  @{N
                  ="SnapName";E={$_.name}},
                                  @{N
                  ="Created";E={$dateHtml}},
                                  @{N
                  ="Description";E={$_.Description}},
                                  @{N
                  ="SizeMB";E={(Get-Snapshot -Name $_.Name -VM $vm).SizeMB}}
                  }
                  $snapTotal = "<h2>Size all snapshots: " + ("{0:f0} GB" -f (($before | Measure-Object -Property SizeMB -Sum).Sum / 1KB)) + "</h2>"
                  $before | ConvertTo-Html -head $a -body $header -title $title -PostContent $snapTotal | Out-String | `
                  foreach {$_.replace("&lt;","<").replace("&gt;",">")} | Out-File $SnapReport

                  $before
                  | Select VMName,SnapName,@{N="Created";E={$_.Created.replace("<p style='color:red;'>","").replace("<p style='color:yellow;'>","").replace("<p style='color:green;'>","").replace("</p>","")}},Description,SizeMB | `
                  Export-Csv "C:\Snapshot-report.csv" -NoTypeInformation -UseCulture
                  #################################################################################################
                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  1 person found this helpful
                  • 6. Re: HTML report
                    jingleharish Enthusiast

                    Luc,

                     

                    Is it possible to have the background of the table colored rather than the text?, as the Yellow text is not visible properly under white background.

                     

                    Thanks in advance,

                    • 7. Re: HTML report
                      LucD Guru
                      Community WarriorsvExpertUser Moderators

                      You only need to change the color attribute in a background-color attribute.

                      Like this

                       

                      #############################################################################################    
                      $stamp
                      = get-date -f ddMMMyy-hhmmss
                      Connect-VIServer $vcservers -Credential $cred   
                      $before = @()
                      $snaptree = @()
                      $SnapReport = "C:\Snapshots-Report-$stamp.html"  
                      #get vm views for all vms with snap
                      Get-View
                      -ViewType virtualmachine | where { $_.snapshot -ne $null } | foreach {
                         
                      $snaptree += $_.snapshot.rootsnapshotlist
                      }
                        
                      $a = "<style>" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
                      $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
                      $a
                      = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
                      $a = $a + "</style>"
                      $header = "<H1>Pending VM Snapshots :: $stamp</H1>"
                      $title = "Vmware Snapshots Reports" 
                            
                      $now = Get-Date
                      $before
                      = $snaptree | %{
                         
                      $vm = get-viobjectbyViView -moref $_.vm
                         
                      $age = ($now - $_.createtime).Days
                         
                      if($age -gt 3){
                             
                      $code = "red"    }
                         
                      elseif($age -gt 2){
                             
                      $code = "yellow"    }
                         
                      else{
                             
                      $code = "green"    }
                         
                      $dateHtml =     '<p style=''background-color:' + $code + ';''>' + $_.CreateTime + '</p>'    $_ | Select @{N="VMName";E={$vm.name}},
                                      @{N
                      ="SnapName";E={$_.name}},
                                      @{N
                      ="Created";E={$dateHtml}},
                                      @{N
                      ="Description";E={$_.Description}},
                                      @{N
                      ="SizeMB";E={(Get-Snapshot -Name $_.Name -VM $vm).SizeMB}}
                      }
                      $snapTotal = "<h2>Size all snapshots: " + ("{0:f0} GB" -f (($before | Measure-Object -Property SizeMB -Sum).Sum / 1KB)) + "</h2>"
                      $before | ConvertTo-Html -head $a -body $header -title $title -PostContent $snapTotal | Out-String | `foreach {$_.replace("&lt;","<").replace("&gt;",">")} | Out-File $SnapReport
                      $before | Select VMName,SnapName,@{N="Created";E={$_.Created.replace("<p style='background-color:red;'>","").replace("<p style='background-color:yellow;'>","").replace("<p style='background-color:green;'>","").replace("</p>","")}},Description,SizeMB | `Export-Csv "C:\Snapshot-report.csv" -NoTypeInformation -UseCulture#################################################################################################

                       

                      You can change the colors by replacing the "red" and "yellow" strings.

                      Consult the HTML Color Chart with 140 Color Names to see what colors are available.

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: HTML report
                        avlieshout Expert
                        VMware Employees

                        You can also define your html looks in a cascading style sheet (css) and apply that to your html code using the -CssUri parameter of the ConvertTo-Html cmdlet.

                        • 9. Re: HTML report
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          Treu, but the problem here is that you have to color some cells conditionally.

                          I don't know how to do that in a CSS

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: HTML report
                            avlieshout Expert
                            VMware Employees

                            Sorry, my bad I didn't read the complete thread.

                             

                            In CSS you could define different classes to have more than one style per HTML element.

                            However, you still need to put the class id into the HTML manually as the ConvertTo-Html cmdlet doesn't support this.

                             

                            But when using cascading style sheet, you can modify your report without having the modify your code afterwards.