Thursday, 5 March 2015

Powershell output handling using HTML

HTML Report in PowerShell -  Using HTML Tables


Here I am going to cover how to handle PowerShell output using 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 a better formatting. Below url help 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.


Scenario : Fetching System Properties :

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 script... Like output heading , table's rows,columns, heading, color, graphs design and location etc: 

Please note the below html tags for basic understandings:

001
002
003
004
005
006
<Table>     : Table Craetion
<tr>        : Table row
<td>        : Table Data
<h1>        : Heading size
<bgcolor>   : Color of the table. You can change as per your wish
<align>     : Alignment of table and table data

 1 - Creating heading for table and output:

001
002
003
004
005
006
007
008
009
$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 customize 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
HOST NAME
OPERATING SYSTEM NAME
SERIAL NO

The format will looks like below:








2 - Process Creation : Using get-wmiobject to retrive the system details and passing the result through html.


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

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
$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 

3 - Report Generation Part : Path Creation, report generation and opening the html file


001
002
003
004
$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 appears as below:











No comments:

Post a Comment