Export csv spits out length only

后端 未结 3 2035
既然无缘
既然无缘 2020-12-21 14:40

I can only get the length when exporting this to csv, how should it be done properly.

$redo = Import-CSV c:\\temp\\testimport.txt | Group-Object email |
fore         


        
相关标签:
3条回答
  • 2020-12-21 15:06

    The method shown for converting data to objects is a very long-hand approach, at least in my circumstance. I'm developing reports integrating with an IBM V7000 SAN storage subsystem and it's CLI, which I can call from PS using putty plink, returns either tabular output (which can be CSV) or list depending upon the query. From either I desire to export the data as CSV. From the list output I'm looping through the result set (one row = one field) and assembling the fields into a string, separating values with commas. (For the tabular output I get to skip this tedious step.)

    The following works to write the output to a CSV file which I can then open as a spreadsheet.

    $fhStream = [System.IO.StreamWriter] "20150527_QALUNTable.csv" 
    $fhStream.WriteLine($stColumnHeadings)
    $fhStream.WriteLine($stColumnValues)
    $fhStream.Close()
    

    Import-Csv works to return the input as an object that I can easily use to prepare my reports (which are assembled from many such files of output, each gathered at a separate point in time -- hence the datestamp prefix).

    There are 57 columns of data here so by converting to a CSV I avoid preparing 57 object statements.

    (Found .Net technique for writing output (fastest) at http://blogs.technet.com/b/gbordier/archive/2009/05/05/powershell-and-writing-files-how-fast-can-you-write-to-a-file.aspx)

    0 讨论(0)
  • 2020-12-21 15:07

    This was exactly what I was looking for. So I'm just going to add, my comment to be sure everyone understands.


    This does NOT work:

    $str_list = @('Mark','Henry','John')
    $str_list | Export-Csv .\ExportStrList.csv -NoType
    

    Because Export-Csv takes Objects and outputs properties. The only properties for a String[ ] is Length, so the CSV file only contains Lengths.

    To fix this, like the last guy said, we need to change the String[ ] into an Object[ ]. The simplest way is with Select-Object.


    Put each String into the Name property of a new Object[ ], like this:

    $str_list = @('Mark','Henry','John')
    $str_list = $str_list | Select-Object @{Name='Name';Expression={$_}}
    $str_list | Export-Csv .\ExportStrList.csv -NoType
    

    Just to re-iterate, Select-Object outputs a custom PSObject that can easily be manipulated. This is very powerful information, use it wisely.

    0 讨论(0)
  • 2020-12-21 15:12

    Export-Csv expects an object (or a list of objects) with properties, whereas your command pipeline produces an array of strings. If you feed this array into Export-Csv the cmdlet takes the properties of each given item (which is only Length for strings) and writes those properties to the output file.

    You need to build a list of objects with the desired properties instead, e.g.:

    Import-CSV c:\temp\testimport.txt `
      | Group-Object email `
      | select @{n="Name";e={$_.Name}},@{n="Group";e={($_.Group | %{$_.group}) -join ', '}} `
      | Export-CSV c:\temp\test.csv -NoTypeInformation
    
    0 讨论(0)
提交回复
热议问题