PowerShell Tip: HTML reports using CSS
Summary
This TechNet Wiki is to demo writing CSS, HTML and PowerShell for enhanced reports.
Very often clients needs reports about some services in Infrastructure. So, this Wiki will show how to quickly make HTML reports.
Tools
- Microsoft Web Expression 4
- PowerShell ISE
help
#Microsoft.PowerShell.Utility help ConvertTo-Html -Examples help ConvertTo-Html -Detailed |
You can try EnhancedHTML2 module shared by Don Jones as well. This makes few tasks easy.
PS C:\Temp> Get-Command ConvertTo-EnhancedHTML CommandType Name ModuleName ----------- ---- ---------- Function ConvertTo-EnhancedHTML EnhancedHTML2 |
PS C:\> Get-Help ConvertTo-EnhancedHTML NAME ConvertTo-EnhancedHTML SYNTAX ConvertTo-EnhancedHTML -HTMLFragments <string[]> [-jQueryURI <string>] [-jQueryDataTableURI <string>] [-CssStyleSheet <string[]>] [-Title <string>] [-PreContent <string>] [-PostContent <string>] [<CommonParameters>] ConvertTo-EnhancedHTML -HTMLFragments <string[]> [-jQueryURI <string>] [-jQueryDataTableURI <string>] [-CssUri <string[]>] [-Title <string>] [-PreContent <string>] [-PostContent <string>] [<CommonParameters>] ALIASES None REMARKS None |
Task 1: Retrieve Services from localhost and save as HTML and apply CSS
#region $service = Get-Service | Select Name , Status , DisplayName | ConvertTo-Html -Fragment #endregion #region ConvertTo-Html -Body $service ` -Title 'Services' ` -PreContent '<h4>Localhost Services</h4>' ` -CssUri C:\Temp\STYLE.CSS | Out-File C:\Temp\Service.html #endregion #region ii C:\Temp\Service.html #endregion |
Output
Task 2: Highlight Stopped Services - Red Color
Get-service | Select Name , DisplayName , Status |ConvertTo-Html -Title "Services" -Body "Localhost Service" | foreach {if($_ -like "*<td>Running</td>*"){$_ -replace "<tr>", "<tr bgcolor=green>"} elseif($_ -like "*<td>Stopped</td>*"){$_ -replace "<tr>", "<tr bgcolor=red>"} else{$_}} | Out-File c:\services.html |
Output
Task 3: Styling your HTML using CSS
Simple CSS code:
body { background:white; font-family:Tahoma; } th { background:aqua; color:black; } td { border:1px Solid black; } |
The above code will style HTML body, table header and table data.
Task 4: Styling your HTML using CSS - Convertto-EnhancedHTML
In this example we have used SharePoint farm for testing:
$SPFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local #region - Alternate Access Mapping $AAM = $SPFarm.AlternateUrlCollections | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ Alternate Access mappings</h4>' #endregion #region - SharePoint Build Version $buildversion = $SPFarm.BuildVersion | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Build Version</h4>' #endregion #region $Servers = $SPFarm.Servers | Select Address , DisplayName , ID , Status | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Farm Servers</h4>' #endregion #region $SPFarmFeatures = $SPFarm.FeatureDefinitions | ? {$_.Scope -eq 'Farm'} | Select DisplayName , ID , Status , CompatibilityLevel | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Farm Feature Definitions</h4>' #endregion #region $SPWebFeatures = $SPFarm.FeatureDefinitions | ? {$_.Scope -eq 'Web'} | Select DisplayName , ID , Status , CompatibilityLevel | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Web Feature Definitions</h4>' #endregion #region $SPSiteFeatures = $SPFarm.FeatureDefinitions | ? {$_.Scope -eq 'Site'} | Select DisplayName , ID , Status , CompatibilityLevel | ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Site Feature Definitions</h4>' #endregion #region $SPServiceProxy = $SPFarm.ServiceProxies| ConvertTo-EnhancedHTMLFragment -EvenRowCssClass 'even' ` -OddRowCssClass 'odd' ` -MakeHiddenSection ` -PreContent '<h4>+ SharePoint Service Proxy</h4>' #endregion ConvertTo-EnhancedHTML -HTMLFragments $AAM , $buildversion , $Servers , $SPFarmFeatures , $SPWebFeatures ,` $SPSiteFeatures , $SPServiceProxy ` -CssUri C:\Temp\StyleSheet.css | Out-File C:\Temp\Farm.html -Encoding ascii |
- When users hover the mouse over the header the cursor changes to pointer and font turns blue.
- This collapse and expands the content.
CSS Code:
body { background-color:white; font-family:Tahoma; font-size:10pt; } th { background-color:green; color:White; } table , th , td { border:1px solid green; } h4 { border:30px; } h4:hover { color:blue; cursor:pointer; } tr:hover { background-color:#00FF00; } |