powershell foreach write data to unique file with unique content

Chad 20 Reputation points
2023-11-21T21:49:22.2433333+00:00

I'm no expert, so thanks in advance. also ignore formatting, i'll format properly when it works.

basic requirement: script to parse json data, find unique id, write the json data found for unique id into a file named according to the id

current script, produces the files uniquely named, but only with the last plugin ID

{"plugin": 57582, "plugin_name": "SSL Self-Signed Certificate", "dns_name": "server1.dev.com", "age": 159}]}}

this script has multiple foreach loops, I understand that not all of those are necessarily needed.

$json = Get-Content C:\temp\json_data.json | ConvertFrom-Json
$uniqueids = $json.response.items.plugin | Get-Unique
$uniqueservers = $json.response.items.dns_name | Get-Unique
#-----------------------------------------------------------------------------------------------------------------------------
foreach ($id in $uniqueids) {

#adding HTML table row
foreach($id in $json.response.items) {
  $table= "<tr> <td> $($id.plugin) </td> <td> $($id.plugin_name) </td> <td> $($id.dns_name) </td> <td> $($id.age) </td> </tr>"
$line = $table | select -Unique

#HTML Table header
$html="
<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> "
#write html tables
$html+= "$line"
#close HTML tags
$html +="</table></body></html>"

#Write data to HTML file
#$html >c:\temp\$id.plugin.html

foreach ($id in $uniqueids) {
    $html >c:\temp\plugin.$id.html

}
}
}
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2023-11-22T19:19:37.0433333+00:00

    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. 
    
        ""
    }
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.