i'll format properly when it works.
The proper formatting will help you write the code. The trap that I see somewhat regularly is a user writes code, usually in the format of "cmdlet | cmdlet | cmdlet" and they don't get the results they expect, and they don't know why it doesn't work.
The trick is to write the code in chunks, get it working and then add in additional functionality. Displaying variable contents along the way really helps.
I've never had a need to work with json, so this is how I got it working. (I think this is what you want.)
Start small and build the file names.
$json = Get-Content C:\temp\test.json | ConvertFrom-Json
$uniqueids = $json.response.items.plugin | Get-Unique
foreach ($id in $uniqueids) {
"Processig $id"
$filename = "c:\temp\plugin.{0}.html" -f $id
"File name to create is {0}" -f $filename
""
}
Then gather the data.
$json = Get-Content C:\temp\test.json | ConvertFrom-Json
$uniqueids = $json.response.items.plugin | Get-Unique
foreach ($id in $uniqueids) {
"Processig $id"
$filename = "c:\temp\plugin.{0}.html" -f $id
"File name to create is {0}" -f $filename
$LineItems = $json.response.items | where-object -property plugin -eq $id
"We found these line items."
$LineItems
""
}
Build your table rows.
$json = Get-Content C:\temp\test.json | ConvertFrom-Json
$uniqueids = $json.response.items.plugin | Get-Unique
foreach ($id in $uniqueids) {
"Processig $id"
$filename = "c:\temp\plugin.{0}.html" -f $id
"File name to create is {0}" -f $filename
$LineItems = $json.response.items | where-object -property plugin -eq $id
"Table rows for $id."
foreach ($li in $LineItems) {
$row = "<tr> <td> $($li.plugin) </td> <td> $($li.plugin_name) </td> <td> $($li.dns_name) </td> <td> $($li.age) </td> </tr>"
$row
}
""
}
And finally wrap in the html and output the files.
cls
$json = Get-Content C:\temp\test.json | ConvertFrom-Json
$uniqueids = $json.response.items.plugin | Get-Unique
$header = "<Html><Body>
<Table border=1 style='border-collapse: collapse;width:50%;text-align:center'>
<th>Plugin ID</th> <th>Description</th> <th>Server</th> <th>Age</th>`n"
$trailer = "</table></body></html>" # note `n for newline
foreach ($id in $uniqueids) {
"Processig $id"
$filename = "c:\temp\plugin.{0}.html" -f $id
"File name to create is {0}" -f $filename
$LineItems = $json.response.items | where-object -property plugin -eq $id
$html = $header
foreach ($li in $LineItems) {
$row = "<tr> <td> $($li.plugin) </td> <td> $($li.plugin_name) </td> <td> $($li.dns_name) </td> <td> $($li.age) </td> </tr>`n" # note `n for newline
$html += $row
}
$html += $trailer
"Here is our html"
$html
$html | out-file $filename # And create our files.
""
}