How To Create HTML Reports In PowerShell

Intro:

Hi friends!!

Today we will discuss creating HTML reports using Powershell. This will be a short blog.

I will share some examples on how to create various html reports using powershell, then you can refer to it for further experiments.

So lets start..

What will we do ?

So basically we will create 3 different reports for a single computer/server as mentioned below:

  1. Generate all the installed HotFix details
  2. Generate all the services status
  3. Generate installed software inventory

HotFix Report – in HTML

Here we will generate a html report that will hold all the installed HotFix details.

So to generate the same use below Powershell code

Clear-Host
$css = @"
<style>

    h1 {

        font-family: Arial, Helvetica, sans-serif;
        color: #e68a00;
        font-size: 28px;

    }

    
    h2 {

        font-family: Arial, Helvetica, sans-serif;
        color: #000099;
        font-size: 16px;

    }

    
    
   table {
		font-size: 12px;
		border: 0px; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	
    td {
		padding: 4px;
		margin: 0px;
		border: 0;
	}
	
    th {
        background: #395870;
        background: linear-gradient(#49708f, #293f50);
        color: #fff;
        font-size: 11px;
        text-transform: uppercase;
        padding: 10px 15px;
        vertical-align: middle;
	}

    tbody tr:nth-child(even) {
        background: #f0f0f2;
    }
    


    #CreationDate {

        font-family: Arial, Helvetica, sans-serif;
        color: #ff3300;
        font-size: 12px;

    }



</style>
"@

#Get the name of the computer
$ComputerName = "<h1>Computer name: $env:computername</h1>"

#Get the hotfix
$hotfix = Get-HotFix | Sort-Object -Descending | Select-Object csname, hotfixid, Caption, InstalledOn, InstalledBy | ConvertTo-Html -PreContent "<h1>Hotfix Report</h1>"

#Create the html
ConvertTo-Html -Title "Hotfix" -Body "$ComputerName,$hotfix"  -Head $css -PostContent "<p id='CreationDate'>Creation Date: $(Get-Date)</p>" | Out-File C:\Temp\hotfix.html

You should get a report generated in C:\Temp\hotfix.html and it will looks like below:

Awesome!! So the first report is completed.

Services Report – in HTML

Here we will generate a html report that will hold all the windows service details irrespective of their status.

Note that, services which are stopped  will reflect as Red

So to generate the same use below Powershell code

Clear-Host
#CSS codes
$css = @"
<style>

    h1 {

        font-family: Arial, Helvetica, sans-serif;
        color: #e68a00;
        font-size: 28px;

    }

    
    h2 {

        font-family: Arial, Helvetica, sans-serif;
        color: #000099;
        font-size: 16px;

    }

    
    
   table {
		font-size: 12px;
		border: 0px; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	
    td {
		padding: 4px;
		margin: 0px;
		border: 0;
	}
	
    th {
        background: #395870;
        background: linear-gradient(#49708f, #293f50);
        color: #fff;
        font-size: 11px;
        text-transform: uppercase;
        padding: 10px 15px;
        vertical-align: middle;
	}

    tbody tr:nth-child(even) {
        background: #f0f0f2;
    }
    


    #CreationDate {

        font-family: Arial, Helvetica, sans-serif;
        color: #ff3300;
        font-size: 12px;

    }



    .StopStatus {

        color: #ff0000;
    }
    
  
    .RunningStatus {

        color: #008000;
    }




</style>
"@

#Get the name of the computer
$ComputerName = "<h1>Computer name: $env:computername</h1>"

#Get the services and format
$services = Get-Service | Select-Object Name, DisplayName, Status | ConvertTo-Html -PreContent "<h2>Services Information</h2>"
$services = $services -replace '<td>Running</td>', '<td class="RunningStatus">Running</td>'
$services = $services -replace '<td>Stopped</td>', '<td class="StopStatus">Stopped</td>'

#Create the HTML
$report = ConvertTo-Html -Body "$ComputerName $services" -Head $css -Title "Service Report" -PostContent "<p id='CreationDate'>Creation Date: $(Get-Date)</p>"
$report | Out-File C:\Temp\services_report.html

You should get a report generated in C:\Temp\services_report.html

And the report should look like below.

Awesome!! So we are done with our second report. Let’s go to the 3rd one.

Software Inventory Report – in HTML

Here we will generate a HTML report of all the installed softwares.

So to generate the same use below code

Clear-Host
$css = @"
<style>

    h1 {

        font-family: Arial, Helvetica, sans-serif;
        color: #e68a00;
        font-size: 28px;

    }

    
    h2 {

        font-family: Arial, Helvetica, sans-serif;
        color: #000099;
        font-size: 16px;

    }

    
    
   table {
		font-size: 12px;
		border: 0px; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	
    td {
		padding: 4px;
		margin: 0px;
		border: 0;
	}
	
    th {
        background: #395870;
        background: linear-gradient(#49708f, #293f50);
        color: #fff;
        font-size: 11px;
        text-transform: uppercase;
        padding: 10px 15px;
        vertical-align: middle;
	}

    tbody tr:nth-child(even) {
        background: #f0f0f2;
    }
    


    #CreationDate {

        font-family: Arial, Helvetica, sans-serif;
        color: #ff3300;
        font-size: 12px;

    }



</style>
"@

#Get the name of the computer
$ComputerName = "<h1>Computer name: $env:computername</h1>"

#Get the installed softwares
$softwares = Get-CimInstance Win32_Product -Property Name, Caption, Vendor, Version | Select-Object Name, Caption, Vendor, Version  | ConvertTo-Html -PreContent "<h1>Installed Softwares</h1>"

#Create the HTML reports
ConvertTo-Html -Title "Software Inventory" -Body "$ComputerName,$softwares" -Head $css -PostContent "<p id='CreationDate'>Creation Date: $(Get-Date)</p>" | Out-File C:\Temp\Software_inventory.html

You should get the report in C:\Temp\Software_inventory.html

And it looks like the following!

Great!! So we generated all the 3 reports. Go through the codes and experiment, I am sure you will be able to create better version of it,

That’s it for today!!! Will see you in my next blog

Take care!!