VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

converto_html use in powercli report.

Hi Luc ,

I am trying to find some inventory information using convertto-html .

this is report is based on the input provided as esxi name .

i am trying to use following way  need help in  finding expression for datastore with less than 10 percent capacity.  (orange line)

and all snapshots in cluster (for some resons not getting proper output)(blue line)

$esxi_tobechecked=read-host "specify esxi name"

$esxi=get-vmhost -name $esxi_tobechecked

$path=read-host "provide path"

$report = @()

$output = New-Object -TypeName PSObject

            $output|Add-Member -MemberType NoteProperty -Name 'cluster' -Value (Get-Cluster -VMHost $esxi).name

            $output|Add-Member -MemberType NoteProperty -Name ' datastores' -Value (Get-Datastore -RelatedObject(get-cluster -VMHost $esxi)|?{$_.FreeSpaceGB -le )

            $output|Add-Member -MemberType NoteProperty -Name 'all snapshots in cluster ' -Value (get-vm -Location(Get-Cluster -VMHost $esxi)|get-snapshot|select name)

$report += $output

           

           

           

            $report|ConvertTo-Html|Out-File -FilePath "$path\report.html"

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can do a join even with that code.

But I don't think that this is the ideal way to report on two semi-independent features like datastores and snapshots.

$esxi = Get-VMHost -name $esxi_tobechecked

$report = @()

$output = New-Object -TypeName PSObject

$output | Add-Member -MemberType NoteProperty -Name 'Cluster' -Value (Get-Cluster -VMHost $esxi).name

$output | Add-Member -MemberType NoteProperty -Name ' Datastores with greate than 50 percent space' -Value (

    (Get-Datastore -RelatedObject(Get-Cluster -VMHost $esxi) |

    Where-Object{($_.FreeSpaceGB)/($_.CapacityGB)*100 -ge 50} |

    Select -ExpandProperty Name) -join ' | '

)

$output | Add-Member -MemberType NoteProperty -Name  'Snapshots in cluster ' -Value (

    (Get-VM -Location(Get-Cluster -VMHost $esxi) | Get-Snapshot | %{

        "{0} {1} {2}" -f $_.Name,$_.VM.Name,$_.Created

    }) -join ' | '

)

$report += $output

$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

$esxi_tobechecked = Read-Host "specify esxi name"

$esxi = Get-VMHost -name $esxi_tobechecked

$path = Read-Host "provide path"

$report = @()

$cluster = Get-Cluster -VMHost $esxi

$output = New-Object -TypeName PSObject

$output|Add-Member -MemberType NoteProperty -Name 'cluster' -Value $cluster.name

$output|Add-Member -MemberType NoteProperty -Name 'datastores' -Value (Get-Datastore -RelatedObject $cluster | where{$_.FreeSpaceGB -le ($_.CapacityGB * 0.1)}).Name

$output|Add-Member -MemberType NoteProperty -Name 'all snapshots in cluster ' -Value (((Get-VM -Location $cluster | Get-Snapshot).Name) -join '|')

$report += $output

$report | ConvertTo-Html | Out-String | Out-File -FilePath "$path\report.html"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks iam checking this .

could yu suggest if out-string has any impact of changing output from  sytem.object to any other form .

as shown below

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That seems to indicate you have more than 1 datastore that passes the test.

In that case a -join operator, like with the snapshots, would fix that.

$esxi_tobechecked = Read-Host "specify esxi name"

$esxi = Get-VMHost -name $esxi_tobechecked

$path = Read-Host "provide path"

$report = @()

$cluster = Get-Cluster -VMHost $esxi

$output = New-Object -TypeName PSObject

$output|Add-Member -MemberType NoteProperty -Name 'cluster' -Value $cluster.name

$output|Add-Member -MemberType NoteProperty -Name 'datastores' -Value (((Get-Datastore -RelatedObject $cluster | where{$_.FreeSpaceGB -le ($_.CapacityGB * 0.1)}).Name) -join '|')

$output|Add-Member -MemberType NoteProperty -Name 'all snapshots in cluster ' -Value (((Get-VM -Location $cluster | Get-Snapshot).Name) -join '|')

$report += $output

$report | ConvertTo-Html | Out-String | Out-File -FilePath "$path\report.html"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

to be very specific i want to check what is wrong in below lines . just changed logic for datastores  for  free space > 50 percent to get array of datastores and similary we have array of snapshots .

$esxi = Get-VMHost -name $esxi_tobechecked

$report = @()

$output = New-Object -TypeName PSObject

            $output|Add-Member -MemberType NoteProperty -Name 'Cluster' -Value (Get-Cluster -VMHost $esxi).name

            $output|Add-Member -MemberType NoteProperty -Name ' Datastores with greate than 50 percent space' -Value (Get-Datastore -RelatedObject(get-cluster -VMHost $esxi)|

            Where-Object{($_.FreeSpaceGB)/($_.CapacityGB)*100 -ge 50})

            $output|Add-Member -MemberType NoteProperty -Name  'Snapshots in cluster ' -Value (get-vm -Location(Get-Cluster -VMHost $esxi)|get-snapshot|select name,vm,created)

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

what i am stuck is that there is no way to use -join operator if code is written as mentioned my prevois response .

and since iam getting output like below it seems code syntax is correct but no way to use join operator.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can do a join even with that code.

But I don't think that this is the ideal way to report on two semi-independent features like datastores and snapshots.

$esxi = Get-VMHost -name $esxi_tobechecked

$report = @()

$output = New-Object -TypeName PSObject

$output | Add-Member -MemberType NoteProperty -Name 'Cluster' -Value (Get-Cluster -VMHost $esxi).name

$output | Add-Member -MemberType NoteProperty -Name ' Datastores with greate than 50 percent space' -Value (

    (Get-Datastore -RelatedObject(Get-Cluster -VMHost $esxi) |

    Where-Object{($_.FreeSpaceGB)/($_.CapacityGB)*100 -ge 50} |

    Select -ExpandProperty Name) -join ' | '

)

$output | Add-Member -MemberType NoteProperty -Name  'Snapshots in cluster ' -Value (

    (Get-VM -Location(Get-Cluster -VMHost $esxi) | Get-Snapshot | %{

        "{0} {1} {2}" -f $_.Name,$_.VM.Name,$_.Created

    }) -join ' | '

)

$report += $output

$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks luc , it has worked .i am exposed this way of doing reporting so aware of this way only .if yu could suggest any other way ....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The basic issue is that you want to report on two semi-unrelated components (datastores + snapshots).

Me personally, I would make two separate reports, which you can still combine in one HTML report.

It would just be two different tables on the same HTML page.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

well that sound quite interesting but i dont know how to do it .

if yu  can share some paper or describe  that way here in short.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use 'fragments' for each component, and at the end assemble the fragments together in one HTML file.

I also used a style sheet to give somewhat of a layout to the report.

$esxi = 'MyEsx'

$path = '.'

$head = @'

<style>

body { background-color:#dddddd;

       font-family:Tahoma;

       font-size:12pt; }

td, th { border:1px solid black;

         border-collapse:collapse; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px }

table { margin-left:50px; }

</style>

'@

$cluster = Get-Cluster -VMHost $esxi

$ds = Get-Datastore -RelatedObject(Get-Cluster -VMHost $esxi) |

    Where-Object{($_.FreeSpaceGB)/($_.CapacityGB) -ge 0.5} |

    Select Name |

    ConvertTo-Html -Property Name -Fragment -PreContent '<h2>Datastore more than 50% free space</h2>' |

    Out-String

$snap = Get-VM -Location(Get-Cluster -VMHost $esxi) | Get-Snapshot |

    Select Name,@{N='VM';E={$_.VM.Name}},Created |

    ConvertTo-Html -Property Name,VM,Created -Fragment -PreContent '<h2>Snapshot Info</h2>' |

    Out-String

ConvertTo-HTML -head $head -PostContent $ds,$snap -PreContent "<h1>Report</h1>" |

Out-String | Out-File -FilePath "$path\report.html"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks  Luc iam going to check this .

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thnaks it is working .can it be looped over array of cluster .i just avoided esxi name and used cluster name instead.

like below

$path = 'C:\Users\'

$csv_info = Import-Csv -Path "C:\Users\clusters.csv"

foreach ($line in $csv_info) {

$head = @'

<style>

body { background-color:#dddddd;

       font-family:Tahoma;

       font-size:12pt; }

td, th { border:1px solid black;

         border-collapse:collapse; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px }

table { margin-left:50px; }

</style>

'@

$y=get-cluster -name $line.clustername

$cluster=get-cluster $y | select name|ConvertTo-Html -Property Name -Fragment -PreContent '<h2>$cluster.name</h2>' |

    Out-String

$ds = Get-Datastore -RelatedObject $cluster |

    Where-Object{($_.FreeSpaceGB)/($_.CapacityGB) -le 0.1} |

    Select Name |

    ConvertTo-Html -Property Name -Fragment -PreContent '<h2>Datastore with less than 10 percent space.</h2>' |

    Out-String

$snap = Get-VM -Location $cluster  | Get-Snapshot |

    Select Name,@{N='VM';E={$_.VM.Name}},Created |

    ConvertTo-Html -Property Name,VM,Created -Fragment -PreContent '<h2>Snapshot Info</h2>' |

    Out-String

ConvertTo-HTML -head $head -PostContent $ds,$snap,$cluster -PreContent "<h1>Report</h1>" |

Out-String | Out-File -FilePath "$path\report12.html"

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you are trying to do here.

Do you want the report for all clusters in 1 HTML file?

Or do you want to have 1 HTML file per cluster?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

report for all clusters in one html file.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$path = 'C:\Users\'

$head = @'

<style>

body { background-color:#dddddd;

       font-family:Tahoma;

       font-size:12pt; }

td, th { border:1px solid black;

         border-collapse:collapse; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px }

table { margin-left:50px; }

</style>

'@

$fragments = @()

foreach ($line in Import-Csv -Path "C:\Users\clusters.csv") {

    $cluster = Get-Cluster -Name $line.ClusterName

    $fragments += ("" | ConvertTo-Html -Property Name -Fragment -PreContent "<h2>Cluster $($cluster.Name)</h2>" |

        Out-String)

    $fragments += (Get-Datastore -RelatedObject $cluster |

        Where-Object{($_.FreeSpaceGB)/($_.CapacityGB) -le 0.1} |

        Select Name |

        ConvertTo-Html -Property Name -Fragment -PreContent '<h3>Datastore with less than 10 percent space.</h3>' |

        Out-String)

    $fragments += (Get-VM -Location $cluster  | Get-Snapshot |

        Select Name,@{N='VM';E={$_.VM.Name}},Created |

        ConvertTo-Html -Property Name,VM,Created -Fragment -PreContent '<h3>Snapshot Info</h3>' |

        Out-String)

}

ConvertTo-HTML -head $head -PostContent $fragments -PreContent "<h1>Report</h1>" |

Out-String | Out-File -FilePath "$path\report12.html"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks Luc, Iam checking this .

0 Kudos