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

This topic contains 5 replies, has 3 voices, and was last updated by  bvi1998 . 2 years, 2 months ago.

  • Author
  • #37316

    bvi1998 .

    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?

  • #37317

    Dave Wyatt

    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

    bvi1998 .

    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?


  • #37321

    Dave Wyatt

    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

    bvi1998 .

    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!

You must be logged in to reply to this topic.