Unable to -append to .csv with new-object

This topic contains 3 replies, has 2 voices, and was last updated by  Don Jones 3 years, 8 months ago.

  • Author
    Posts
  • #12119

    src053
    Participant

    All,
    I have a .csv with three columns A B and C
    Column A is named "Serial"
    Colum B is named "Customer"
    Column C is named "Seat"

    The csv will have existing content and I only need to append data which will be added via my powershell script.
    within the script I am creating new objects the following way

    $newData = @{'Serial' = 10; 'Customer' = 'NewCustomerName'; 'Seat' = 100}
    $a = new-object -TypeName PsObject -Property $newData
    $a |export-data "c:\location\thecsv.csv" -append
    

    I get an error:
    Export-Csv : Cannot append CSV content to the following file: C:\test\PSTesting.csv. The
    appended object does not have a property that corresponds to the following column:
    Serial. To continue with mismatched properties, add the -Force parameter, and then retry
    the command.
    At line:1 char:5
    + $a |Export-Csv C:\test\PSTesting.csv -Append
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (Serial:String) [Export-Csv], InvalidOperatio
    nException
    + FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.Power
    Shell.Commands.ExportCsvCommand

    I am sure that those properties exist AND that there is no typo.
    What the problem seems to be is when I do a get-member against the $a its not showing any of those three properties.
    It will display them when I just type $a but it will 'Serial', 'Customer', or 'Seat' as a property in GM.
    Any thoughts on how to create the object so that the properties are part of it?

    This me prototyping to see if I can use PS at scale with a living csv file (I know pretty bad). And not being able to figure out why new objects aren't listing properties in GM is driving me crazy.

  • #12121

    Don Jones
    Keymaster

    Well, your code snippet says Export-Data, but I'll assume you're using Export-CSV?

    And the problem is indeed that the CSV contains a "Serial" column but, at the time you're using it, the object(s) in $a don't contain a Serial property. Your use of Get-Member proves it. So let me run through this on my end.

    If I just take your three lines of code and run them, with no pre-existing CSV file, I get a CSV file with one data row. If I run the code again, it runs without error, and the CSV contains both data lines. So your approach here is fine. You'll notice, if you do that, that the CSV file contains a #comment row listing the data type that you produced – a PSCustomObject. Or that's what happens to me – can you confirm that's what happens to you?

    Now, if I use Notepad to make this CSV file manually:

    "Serial","Customer","Seat"
    "10","NewCustomerName","100"
    "10","NewCustomerName","100"
    

    And then I run your three lines of code again, I still get no error. The CSV lacks the comment row still, but I successfully appended a third line of data to it.

    So in short, this works for me in v3. What am I doing differently from you?

    And yeah, a "living" CSV file is just awful. Too bad you can't use something sensible like an actual database, or even XML, which can at least be modified programmatically. All this mucking about with text. Yuck. Ah well.

  • #12142

    src053
    Participant

    wow
    this is just so confusing. I'm was doing all my protyping on a windows 8.1 Enterprise edition and the appending nor creating csv was working. However, once ran on a win 7 v3 machine the steps mentioned did work...
    Is the powershell on enterprise 8.1 different?

  • #12144

    Don Jones
    Keymaster

    PowerShell is no different across editions, but 8.1 comes with v4, not v3. So there are obviously version differences. Although I ran this in v3 myself on Win8.

You must be logged in to reply to this topic.