TechOnTip Weblog

Run book for Technocrats

Powershell# HTML Report with Multiple Tables

Posted by Brajesh Panda on January 8, 2015

## ConvertTo-Html –Fragment is used.

## Reference http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/01/working-with-html-fragments-and-files.aspx

## Execution ## save the script as html-fragment.ps1 and run as “.\html-fragment.ps1 > html-fragment.htm”

$computername Read-Host “Type computername”

function Get-CSInfo {

param($computername)

$os Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername

$cs =Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername

$bios =Get-WmiObject -Class Win32_BIOS -ComputerName $computername

$props = @{

‘ComputerName’=$computername

‘OS Version’=$os.version

‘OS Build’=$os.buildnumber

‘Service Pack’=$os.sevicepackmajorversion

‘RAM’=$cs.totalphysicalmemory

‘Processors’=$cs.numberofprocessors

‘BIOS Serial’=$bios.serialnumber }

$obj New-Object -TypeName PSObject -Property $props

Write-Output $obj

}

$frag1 Get-CSInfo –computername $computername ConvertTo-Html -As LIST -Fragment -PreContent ‘<h2>Computer Info</h2>’
Out-String

$frag2 Get-WmiObject -Class Win32_LogicalDisk -Filter ‘DriveType=3’ -ComputerName $computername Select DeviceID, Freespace, Size
ConvertTo-Html -Fragment -PreContent ‘<h2>Disk Info</h2>’ Out-String

$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>

‘@

ConvertTo-HTML -head $head -PostContent $frag1,$frag2 -PreContent “<h1>Hardware Inventory for SERVER2</h1>”

# Output Screenshot



8 Responses to “Powershell# HTML Report with Multiple Tables”

  1. matthewquickenden said

    Here is a link to post about an HTML reporting module. It’s a series of functions that help generate HTML code easily from within your powershell scripts. I am actually working on some additional functions as well, so stay tuned.

    Enjoy
    http://www.azurefieldnotes.com/2016/08/04/powershellhtmlreportingpart1/

  2. domiRMK said

    Thank you very much! I searched a long time for this. Great 🙂

  3. sixnetworks said

    What if I have a “variable” with multiple values. Basically I did what you did, and it only displays one of the values.
    If I do a write-host $virtualsws, it displays all three virtual switches.

    If I do the following:
    foreach ($virtswitch in (get-vmhost | Get-Virtualswitch | select name, mtu))
    {$virtswitch = $virtswitch -replace ‘[@{}]’}

    $props05 = @{
    ‘Virtual Switches’=$virtswitch
    ‘vSwitch0 MTU’=$vSwitch0Mtu

    It displays only one of the switches.
    Would appreciate if you can guide me on the right direction. I have multiple values with the same problem. Trying to build a report for ESXi hosts

    Thanks for the help

  4. sixnetworks said

    Brajesh,

    Sorry for the delay. I figured out my issue.
    I was doing the follwoing:
    $NTPSrv = (Get-VMHost | Get-VMHostNTPServer)

    The result in the html file was:
    NTP Servers” System.String[]

    I resolved by changing that code to:
    $NTPSrv = ((Get-VMHost | Get-VMHostNTPServer) -join ‘,’)

    One question, is there a way to set the order of appearance on the html file
    This is what I entered to the script:
    $props02 = @{
    ‘Domain Name’=$DName
    ‘Search Domain Name’=$SName
    ‘Host Name’=$HName
    ‘DNS Servers’=$DNSs
    }

    This is what I get in the html file:
    Host Name Settings Section
    Search Domain Name: Carrol.com
    Host Name: esxi01
    Domain Name: Carrol.com
    DNS Servers: 192.168.x.x,192.168.x.x

    Appreciate your help

Leave a comment