VMware Cloud Community
jingleharish
Contributor
Contributor
Jump to solution

HTML report

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

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
10 Replies
Zsoldier
Expert
Expert
Jump to solution

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.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jingleharish
Contributor
Contributor
Jump to solution

Thanks Luc, That one worked, Smiley Happy

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jingleharish
Contributor
Contributor
Jump to solution

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,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

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.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

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.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos