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



Advertisements

7 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: