How to generate multiple reports in HTML using PowerShell?

Software & CodingMicrosoft TechnologiesPowerShell

To generate or append multiple outputs in the HTML file using PowerShell, we need to use the - Fragment parameter in the ConvertTo-HTML command.

For example, let say we need to generate the computer utilization report which includes the top 5 consuming processes, stopped services, and the disk utilization report. A single report can be generated by sending the output to the ConvertTo-HTML pipeline command.

Example

$Heading = "<h1><center>System Utilization Report</h1></center>"

$procs = Get-Process | Sort-Object -Property CPU -Descending| Select -
First 5 | `
   ConvertTo-Html -Property ProcessName, ID, CPU -Fragment -
PreContent "<h3>High Utilization Processes</h3>"

$services = Get-Service | where{$_.StartType -eq "Disabled"} | `
   ConvertTo-Html -Property Name, Status, StartType -Fragment -
PreContent "<h3>Disabled Services</h3>"

$disks = Get-WmiObject win32_logicaldisk | `
   Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}}
, `
   @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | `
   ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>"
ConvertTo-Html -Body "$Heading $procs $services $disks" | OutFile C:\Temp\SystemUtilizationReport.html

ii C:\Temp\SystemUtilizationReport.html

Output

To add the CSS style to the Report,

$Heading = "<h1><center>System Utilization Report</h1></center>"

$procs = Get-Process | Sort-Object -Property CPU -Descending| Select -
First 5 | `
   ConvertTo-Html -Property ProcessName, ID, CPU -Fragment -
PreContent "<h3>High Utilization Processes</h3>"

$services = Get-Service | where{$_.StartType -eq "Disabled"} | `
   ConvertTo-Html -Property Name, Status, StartType -Fragment -
PreContent "<h3>Disabled Services</h3>"

$disks = Get-WmiObject win32_logicaldisk | `
   Select DeviceID, @{N='FreeSpace(GB)';E={[math]::Round(($_.FreeSpace/1GB),2)}}
, `
   @{N='TotalSpace(GB)';E={[math]::Round(($_.Size/1GB),2)}} | `
   ConvertTo-Html -Fragment -PreContent "<h3>Disk Information</h3>"

ConvertTo-Html -Body "$Heading $procs $services $disks" -Head $cssstyle | OutFile C:\Temp\SystemUtilizationReport.html

ii C:\Temp\SystemUtilizationReport.html

Output

raja
Published on 01-Sep-2021 08:17:20
Advertisements