HTML Report in PowerShell –  Using HTML Table.

Below article explains how to handle PowerShell output using an HTML table.

Benefits of Html output: We will be able to do the different types of formatting using Html like table creation, coloring, font style, etc., but we need to know some basic Html language to do it in a better way. The below URL helps you to learn the basics of Html. So if you are a beginner in Html language, please go through the below URL and understand the basics of Html to start creating repost in PowerShell.

Learn Html Basic – https://www.w3schools.com/

I will explain the HTML reporting using the below scenario based example

Scenario: Fetching few Operating System details from a computer :

Through this example, I am using Get-Wmiobject to get some system details and passing the output to Html tables. Here we have to decide the output design before creating the script… Like output heading, table’s rows, columns, heading, color, graphs design and location, etc;

Please note the below Html tags for basic understandings:

<Table>     : Table Creation
<tr>        : Table row
<td>        : Table Data
<th>        : Table Heading
<h1>        : Heading size
<bgcolor>   : Color of the table. You can change as per your wish
<align>     : Alignment of table and table data

First we will create the heading for table and output as showing below

$Table = { '<table width=40% align=center>'
'<th align="center"> <h3><font color=red>SYSTEM PROPERTIES<h3></th>'
'</table>'
'<table border="1" width=40% align=center>'
'<tr bgcolor=skyblue>'
'<th>HOST NAME</th><th>OPERATING SYSTEM NAME</th><th>SERIAL NO</th>' }

This will be the head part of your output. Table width, height, and alignment can be customized as per your requirement. In this example, the output heading will be SYSTEM PROPERTIES and I am fetching the below three details of a computer

HOSTNAME
OPERATING SYSTEM NAME
SERIAL NO

The format will look like below:

Using then below get-wmiobject cmdlet we will retrieve the system details and passing the result through Html.

$Sysdetails = Get-WmiObject -Class win32_operatingsystem -ComputerName $_

Process Creation – This is the processing part of this script and forming the html format

$process=
{
Get-Content $env:USerProfile\desktop\pc.txt | foreach {
'<tr>'
'<td align="center" >{0}</td>' -f $_
if(!(Test-Connection -Cn $_ -BufferSize 16 -Count 1 -ea 0 -quiet))
{
$failed = "Failed to Ping"
'<td bgcolor = orange align = center>{0}</td><td bgcolor = orange align = center>{1}</td>' -f $failed,$failed
}
else
{
$Error.Clear()
$Sysdetails = Get-WmiObject -Class win32_operatingsystem -ComputerName $_
if($Error[0])
{
$unknown = "Unknown"
'<td bgcolor = yellow align = center>{0}</td> <td bgcolor = yellow align = center>{1}</td>' -f $unknown,$unknown
}
else
{
'<td bgcolor=lightgreen align=center>{0}</td> <td bgcolor=lightgreen align=center>{1}</td>' -f $Sysdetails.Caption,$Sysdetails.SerialNumber
}
}'<tr>'
$Sysdetails = $null
}
# Foreach End

}
#Process End
$Tableend

And below is the report generating Part: Path Creation, report generation, and opening the Html file

Please note that the output will be saved on your desktop as per the below example with the name “SysDetails.html”

$Path = "$env:USerProfile\desktop\SysDetails.html"
ForEach-Object -Begin $table -Process $process -End $tableend |
Set-Content -Path $Path -Encoding ascii
Invoke-Expression $Path

Finally, the output of the script will appear as below:

You can download entire script from the below URL

 

 

Thank You !!!