PowerShell - How To Develop Interactive Logs

In this article, we will look for developing a logging mechanism for PowerShell automation components that can provide logs not just in the form of a flat file, but also as an interactive HTML format. This best part of the story is we can design this HTML log format using whatever HTML/CSS elements to make as intuitive as possible.

For the sake of clear understanding, I am going to explain the complete process in various steps-

We need to download and install the “EnhancedHTML2” module.

In Step 1, we can see the command to download & install the module. We can use this command using PowerShell Console, or we can download & install this module manually as shown in Step 2, from the PowerShell Gallery

PowerShell - How To Develop Interactive Logs 

In Step 3, we have added the code to the query for all Lists in SharePoint Site. I have kept it simple & straight for this demo.

In Step 4, we have added the code to build a collection keep list of SharePoint Lists using a PowerShell hash table as shown below-

PowerShell - How To Develop Interactive Logs

In Step 5 and 6, we can see the implementation of “EnhancedHTML2” module.

In Step 5, we will convert the data collection (Row & Column format) to “EnhancedHTMLFragment” by using “ConvertTo-EnhancedHTMLFragment” cmdlet. Similarly, we can create any number of HTML Fragments as per our need.

This cmdlet uses a switch “PreContent” that allows you to any HTML snippet before the fragment adds to the HTML outcome.

“-MakeTableDynamic” switch allows the cmdlet to embed basic HTML plumbing like Search, Pagination and so on to the HTML outcome

In Step 6, we will append all the HTML Fragments to build a single HTML file by using “ConvertTo-EnhancedHTML”cmdlet.

This cmdlet has got the “HTMLFragments” parameter that will accept comma separated values of names of the Fragments that you want to include in the HTML document.

Another important parameter “CssUri” accepts the path of CSS file that will define the styling attributes for the HTML page.

The “Out-File” parameter defines the path to the save the HTML Log file.

PowerShell - How To Develop Interactive Logs

In Step 7, we will call the method to execute the above steps.

PowerShell - How To Develop Interactive Logs

In Step 8, the dll files are shown that we need to include to the script to make SharePoint Calls work.

PowerShell - How To Develop Interactive Logs

Step 9 shows the CSS file that we referred with “ConvertTo-EnhancedHTML” in Step 6.

PowerShell - How To Develop Interactive Logs

Step 10 shows the HTML outcome log file generated after we successfully executed the steps above

PowerShell - How To Develop Interactive Logs

In Step 11 & 12 we can explore the HTML generated as part of this execution and it is amazing to see how this HTML is structured by adding references to the required JavaScript files and body elements.

It can be clearly noticed the generated HTML body content is in the form of the table structure; this happened since we specified “-As Table” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5.

Also, we can notice the “<h1>” tag in the HTML body. It is the same content that we included to the fragment using “-PreContent” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5

PowerShell - How To Develop Interactive LogsPowerShell - How To Develop Interactive Logs

And finally, we can see the HTML based interactive logs in Step 13, as shown below. This would be a feature-rich HTML Page and we can even further add more feature as per our needs, thought this would cost additional effort.

PowerShell - How To Develop Interactive Logs

Code Snippet
  1. function Get - Interactive - Log() {  
  2.     $web = $clientContext.Web;  
  3.     $listColl = $web.Lists;  
  4.     $clientContext.Load($listColl);  
  5.     $clientContext.ExecuteQuery();  
  6.     foreach($list in $listColl) {  
  7.         write - host - ForegroundColor Green "List Name: "  
  8.         $list.Title " ID: "  
  9.         $list.Id  
  10.         $logKey = [guid]::NewGuid()  
  11.         $logData = "$($SiteUrl)|$($list.Title)|$($list.Id)"  
  12.         $spListsAuditLog.Add($logKey, $logData);  
  13.     }  
  14.     if ($spListsAuditLog.Count - gt 0) {  
  15.         $finalExportPath = "$($ScriptPath)\Logs\$($dateTimeStamp)"  
  16.         New - Item - ItemType Directory - Force - Path "$($finalExportPath)" | Out - Null  
  17.         $fileName = "ListsManagement"  
  18.         $filePath = "$($finalExportPath)\$($fileName)"  
  19.         $spListsCollection = $spListsAuditLog.getEnumerator()  
  20.         `  
  21.   
  22. | Select @{Name="Site Url";Expression={($_."value").Split('|')[0]}}, `  
  23.         @ {  
  24.             Name = "List Name";  
  25.             Expression = {  
  26.                 ($_.  
  27.                     "value").Split('|')[1]  
  28.             }  
  29.         }, `  
  30.   
  31. @{Name="List Id";Expression={($_."value").Split('|')[2]}} ` | Sort - Object "List Name"  
  32.         $interactiveLogPath = "$($finalExportPath)\$($fileName).htm"  
  33.         $interactiveLogFragment = $spListsCollection | ConvertTo - EnhancedHTMLFragment - As Table - PreContent "<h1>Lists Management Report</h1>" - MakeTableDynamic  
  34.         ConvertTo - EnhancedHTML - HTMLFragments $interactiveLogFragment - CssUri "$($scriptPath)\styles2.css" | Out - File $interactiveLogPath  
  35.     }  
  36. }  
That is all for this demo.

Hope you find it helpful.


Similar Articles