VMware Cloud Community
AdamUK
Enthusiast
Enthusiast
Jump to solution

PowerCLI - HTML formatting and specifying column widths

Hi

I have the script below which LucD has helped a lot with, but I would like to be able to define the width of each table column so I can make their width specific to the content they contain.  My script is as follows and it currently makes each column of equal width.  Could someone please advise if I can configure the script to specify specific column widths:

$htmlvmdata = "<style>"

$htmlvmdata = $htmlvmdata + "BODY{background-color:peachpuff;}"

$htmlvmdata = $htmlvmdata + "TABLE{table-layout: fixed;width: 1500px;font-size:10px;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse}"

$htmlvmdata = $htmlvmdata + "TH{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:thistle}"

$htmlvmdata = $htmlvmdata + "TD{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:palegoldenrod}"

$htmlvmdata = $htmlvmdata + "BODY{display: block}"

$htmlvmdata = $htmlvmdata + "</style>"

$reportvmdata_html = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef

      foreach ($vm in $vms){

        $infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost

        $infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"

        $infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"

        $infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)

        $infovmdata_html.datacenter = $dc.name

        $infovmdata_html.Name = $vm.name

        $infovmdata_html.toolsstatus = $vm.guest.toolsstatus

        $infovmdata_html.NumCpu = $vm.Summary.config.NumCpu

        $infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024

        $infovmdata_html.guestos = $vm.guest.guestfullname

        $infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name

        $reportvmdata_html += $infovmdata_html

      }

    }

}

($reportvmdata_html | Sort-Object -Property Datacenter,Name | ConvertTo-Html -Head $htmlvmdata -Body "<h1> Report run on: $Dateformat</h1><h2>Total number of VMs across all Data Centres/Clusters: $total_vms</h2>").Replace('|','<br/>') | Out-File $export_html_vmdata

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is more a CSS style issue than a PowerShell issue :smileygrin:

I adapted the table layout definition, then you get the optimal column width by default.

Try like this

$htmlvmdata = @'

<style>

BODY{background-color:peachpuff;}

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

TH{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:thistle}

TD{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:palegoldenrod}

BODY{display: block}

</style>

'@


$reportvmdata_html = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef

      foreach ($vm in $vms){

        $infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost

        $infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"

        $infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"

        $infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)

        $infovmdata_html.datacenter = $dc.name

        $infovmdata_html.Name = $vm.name

        $infovmdata_html.toolsstatus = $vm.guest.toolsstatus

        $infovmdata_html.NumCpu = $vm.Summary.config.NumCpu

        $infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024

        $infovmdata_html.guestos = $vm.guest.guestfullname

        $infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name

        $reportvmdata_html += $infovmdata_html

      }

    }

}


$Dateformat = Get-Date -Format 'dd/MM/yy HH:mm'

$total_vms = $reportvmdata_html.Count

$body = @"

<h1> Report run on: $Dateformat</h1>

<h2>Total number of VMs across all Data Centres/Clusters: $total_vms</h2>

"@


$export_html_vmdata = '.\report.html'

($reportvmdata_html | Sort-Object -Property Datacenter,Name | ConvertTo-Html -Head $htmlvmdata -Body $body).Replace('|','<br/>') |

Out-File -FilePath $export_html_vmdata


Invoke-Item -Path $export_html_vmdata


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

That is more a CSS style issue than a PowerShell issue :smileygrin:

I adapted the table layout definition, then you get the optimal column width by default.

Try like this

$htmlvmdata = @'

<style>

BODY{background-color:peachpuff;}

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

TH{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:thistle}

TD{text-align:center;border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:palegoldenrod}

BODY{display: block}

</style>

'@


$reportvmdata_html = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef

      foreach ($vm in $vms){

        $infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost

        $infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"

        $infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"

        $infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)

        $infovmdata_html.datacenter = $dc.name

        $infovmdata_html.Name = $vm.name

        $infovmdata_html.toolsstatus = $vm.guest.toolsstatus

        $infovmdata_html.NumCpu = $vm.Summary.config.NumCpu

        $infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024

        $infovmdata_html.guestos = $vm.guest.guestfullname

        $infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name

        $reportvmdata_html += $infovmdata_html

      }

    }

}


$Dateformat = Get-Date -Format 'dd/MM/yy HH:mm'

$total_vms = $reportvmdata_html.Count

$body = @"

<h1> Report run on: $Dateformat</h1>

<h2>Total number of VMs across all Data Centres/Clusters: $total_vms</h2>

"@


$export_html_vmdata = '.\report.html'

($reportvmdata_html | Sort-Object -Property Datacenter,Name | ConvertTo-Html -Head $htmlvmdata -Body $body).Replace('|','<br/>') |

Out-File -FilePath $export_html_vmdata


Invoke-Item -Path $export_html_vmdata


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

AdamUK
Enthusiast
Enthusiast
Jump to solution

Thanks, that works perfectly

Reply
0 Kudos
AdamUK
Enthusiast
Enthusiast
Jump to solution

Don't suppose you have also done something with conditional formatting i.e. if an output has a specific value, turn the cell a different colour?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at Re: Generate HTML Report with cell colors


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

Reply
0 Kudos