Output from command is comma separated with no headers - how to format?

Tagged: 

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of bvi1998 . bvi1998 . 5 months, 3 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #37316
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Hi,
    I ran a curl statement and populated a variable named $output.
    Inside the $output variable are values which are comma separated with no headers. I would like to create an html file showing the list of values.
    $output contains something like the following, including the quotation marks:

    "app.something.status", "app.something2.pl", "app.someapp.health"

    I have tried everything. The only success I had in actually getting the data out was to export it to a txt file using:

    $output | out-file c:\scripts\output\k.txt

    Now I have a file which looks exactly like the content of $object, but I am unable to format it in any other way. If I had headers I suppose it might be easier.

    Would someone please give me some ideas?
    Thanks!

    #37317
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    To me, it looks more like a list of values than a CSV object where headers would be appropriate. However, if you know what headers should be associated with those values, then you can use ConvertFrom-Csv:

    $object = $output | ConvertFrom-Csv -Header Field1,Field2,Field3
    

    If it's just a list, then you can split it on the commas and remove the quotation marks to get a string array:

    $array = $output -split '\s*,\s*' -replace '^"|"$'
    
    #37318

    You can try something like

    $newoutput = ConvertFrom-Csv -InputObject $output -Header "header1","header2","header3"

    you beat me 😉

    #37320
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Thanks Both!
    Dave's worked because it listed the 30 or so values into a list. The last part of this is to export it to an html file.... I just tried to pipe to format-list then convertto-html but the values show up as Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry.

    Can you explain the current format after I execute the $array = $output -split '\s*,\s*' -replace '^"|"$' ... What is convertto-html expecting?

    Thanks!

    #37321
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Don't use Format-List here; just pipe objects straight to ConvertTo-Html. In this case, you probably need to construct objects out of your strings, as well, or you will probably get weird results:

    $array | ForEach-Object { [pscustomobject] @{ Value = $_ } } | ConvertTo-Html
    
    #37322
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Perfect!
    I also sorted it using select-object * like this:

    $array | ForEach-Object { [pscustomobject] @{ Value = $_ } } | Sort-Object * | ConvertTo-Html | out-file c:\scripts\output\.output.html

    Thanks! You saved me hours of work!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.