Powershell - Convert an ordered nested dictionary to html

Karim Kanoun 1 Reputation point
2021-12-09T07:25:57.67+00:00

I need to report the number of VM snapshots based on their age. For this, I constructed an ordered dictionary that contain another dictionary like this (json output):

{
  "Less than 24h": {
    "Prod": 15,
    "Other": 11
  },
  "1 day": {
    "Prod": 29,
    "Other": 12
  },
  "2 days": {
    "Prod": 11,
    "Other": 0
  },
  "3 days and more": {
    "Prod": 0,
    "Other": 0
  }
}

I need to convert it to html to be included in a mail.
I find how to convert a "simple" dictionary :

$Body1 = $dict.GetEnumerator()| Select-Object Key,Value | ConvertTo-Html -Fragment | Out-String
$Body1 = $Body1.Replace('Key','Days').Replace('Value','Number of Snapshot')

And that work fine, but not if the values are nested dictionaries.
For nested dictionary the output will be like this :

Days Number of Snapshot
Less than 24h System.Collections.Specialized.OrderedDictionary
1 day System.Collections.Specialized.OrderedDictionary
2 days System.Collections.Specialized.OrderedDictionary
3 days and more System.Collections.Specialized.OrderedDictionary

Is there a way to have a html output like this :

Days Prod Other
Less than 24h 15 11
1 day 29 12
2 days 11 0
3 days and more 0 0
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaveK 1,871 Reputation points
    2021-12-09T10:21:54.587+00:00
    $Header = @"
    <style>
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
    </style>
    "@
    (($Json | ConvertFrom-Json).PSObject.Properties | Select Name -ExpandProperty Value | ConvertTo-HTML -Head $Header -Property Name, Prod, Other).Replace("Name","No of Days") | Set-Content c:\temp\table.html
    

    I got this to work from creating a $json variable using your example data above. I think it outputs as you required. If you need to use the HTML in something like an email, just capture the output to $body rather then to file like I have for testing

    0 comments No comments

  2. Limitless Technology 39,916 Reputation points
    2021-12-15T16:08:22.363+00:00

    Hi @Anonymous

    You can first try converting the dictionary to other formats like hash table and then try converting them to HTML . Like any other data type conversion in PowerShell, we can convert Dictionary to hashtable in a similar way.

    PS C:\> $(Dictionary).GetType() | ft -AutoSize

    You can then use the parameters of ConvertTo-Html to select object properties, to specify a table or list format, to specify the HTML page title, to add text before and after the object, and to return only the table or list fragment, instead of a strict DTD page.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-html?view=powershell-7.2

    Hope this resolves your Query!!


    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.