How to display multiple outputs on an HTML webpage using PowerShell?


Consider you have multiple outputs to display on the webpage using PowerShell and we will use the Convertto-HTML built-in cmdlet for it but to display them properly, we first need to convert each part into fragment using –Fragment parameter.

Let’s just take the simple example without the –Fragment parameter. In the below example, we are going to display the BIOS information, Local disk information, and Automatic but stopped services information.

#To get the BIOS information
Get-CimInstance Win32_BIOS | Select Name, Manufacturer, SerialNumber, Status, Version | ConvertTo-Html | Out-File ComputerInformation.html

#To get the logical disk information
Get-CimInstance Win32_LogicalDisk | where{$_.DriveType -eq '3'} | Select DeviceID, @{N='Total Size(GB)';E={[math]::Round($_.Size/1GB,2)}}, @{N='Free size(GB)';E={[math]::Round($_.Freespace/1GB)}} | ConvertTo-Html| Out-File ComputerInformation.html -Append

#To get the service information
Get-Service | where{($_.StartType -eq "Automatic") -and ($_.Status -eq "Stopped")} | Select Name, DisplayName, StartType, Status | ConvertTo-Html | Out-File ComputerInformation.html -Append

The output of the above script.

The output is correct for the above commands but let's check the HTML file by editing it.

You can check in the above-mentioned snapshot of the edited HTML file, there are three different HTML files appended into a single file but we don’t need the output in the same format because display might be OK but the background of the HTML edited output file is incorrect. It is also very difficult to link the CSS file to the entire HTML content.

As we need a single HTML file and we will use the –Fragment parameter in Convertto-HTML command, which tells the PowerShell not to create separate HTML for each input. We will check the script below for a better understanding.

$bios = Get-CimInstance Win32_BIOS | Select Name, Manufacturer, SerialNumber, Status, Version | ConvertTo-Html –Fragment

$disks = Get-CimInstance Win32_LogicalDisk | where{$_.DriveType -eq '3'} | Select DeviceID, @{N='Total Size(GB)';E={[math]::Round($_.Size/1GB,2)}}, @{N='Free size(GB)';E={[math]::Round($_.Freespace/1GB)}} | ConvertTo-Html -Fragment

$services = Get-Service | where{($_.StartType -eq "Automatic") -and ($_.Status -eq "Stopped")} | Select Name, DisplayName, StartType, Status | ConvertTo-Html -Fragment

ConvertTo-Html -Body "$bios $disks $services" -Title "Computer Information" | Out-File ComputerInformation.html

When you check the output of the above script, the output will remain the same as earlier mentioned.

And now we will check the HTML file by editing it and will check if it has a single HTML file or multiple HTML files appended.

There is only one HTML file created and it is easy to attach CSS style to the script. We will modify the script slightly for CSS effects and attach a new CSS file. Both are mentioned below.

Script

$bios = Get-CimInstance Win32_BIOS | Select Name, Manufacturer, SerialNumber, Status, Version | ConvertTo-Html –PreContent "<h2>BIOS Information</h2>"
–Fragment

$disks = Get-CimInstance Win32_LogicalDisk | where{$_.DriveType -eq '3'} | Select DeviceID, @{N='Total Size(GB)';E={[math]::Round($_.Size/1GB,2)}}, @{N='Free size(GB)';E={[math]::Round($_.Freespace/1GB)}} | ConvertTo-Html –PreContent "<h2>Disk Information</h2>" -Fragment

$services = Get-Service | where{($_.StartType -eq "Automatic") -and ($_.Status -eq "Stopped")} | Select Name, DisplayName, StartType, Status | ConvertTo-Html –PreContent "<h2>Auto Stopped Services</h2>" -Fragment

ConvertTo-Html -Body "$bios $disks $services" -Title "Computer Information" -CssUri .\style1.css | Out-File ComputerInformation.html

CSS file

body{
   background-color: LightGray;
   text-align: center;
   color: blue;
}

h2{
   background-color: DarkOrange;
   color: Cornsilk;
}

table, th, td {
   border: 1px solid black;
}

Output

Updated on: 11-Nov-2020

942 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements