Solved

working with csv objects in powershell

Posted on 2014-01-31
10
920 Views
Last Modified: 2014-01-31
I've been creating a psobject containing two properties and exporting to a csv.
I want to be able to import that data back in, add another record, sort it and then recreate the csv.

How can I do this, preferably in both ver 2 & 3?

Customer Age
-------       -------
John          25


$obj = new-object psobject
$obj | add-member noteproperty CustName "John"
$obj | add-member noteproperty CustAge "25"
$obj | export-csv agedata.csv


$CustInfo = import-csv agedata.csv

$newcust's name and age will be function [string]parameters

$CustInfo += $newCust does not work for me (have seen several examples showing this)
0
Comment
Question by:sirbounty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39824820
As long as $CustInfo is an array $CustInfo += $newCust will work.
If you only exported a single object, of course when read back it will be a single object.  You can convert it with
$CustInfo = @($CustInfo)
and then the following will work.
$CustInfo += $newCust
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824894
I've tried several ways and I end up with:

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824904
Here's a rough example of what I'm trying to pull off...

function newData {
    [Cmdletbinding()]
    param(
    [parameter(Mandatory=$true)]
    [string]$CustName,

    [parameter(mandatory=$true)]
    [string]$CustAge
    )

    process {
        $obj = New-Object psobject
        $obj | add-member noteproperty CustName $CustName
        $obj | add-member noteproperty CustAge $CustAge
        Write-Output $obj
    }
    
}


$obj = new-object psobject
$obj | add-member noteproperty CustName "John"
$obj | add-member noteproperty CustAge "25"
$obj | export-csv agedata.csv

$CustInfo = Import-Csv agedata.csv
$newCust = newData "Ted" "33"

$combineData = $CustInfo + $newCust
$combineData

Open in new window

0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 40

Accepted Solution

by:
footech earned 250 total points
ID: 39824916
I got that too until I ran
$CustInfo = @($CustInfo)
So the order would be
$CustInfo = import-csv agedata.csv
$CustInfo = @($CustInfo)
$newcust = new-object psobject -Property @{ CustName="hank"; CustAge="23"}
$CustInfo += $newcust

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824917
and of course, these end up being simply pscustomobjects...
I'm open to outputting them a different way, but I need to be able to
 -write to a csv formatted file
 -later read it in
 -add a new entry to it
 -sort it based on the first column
 -re-save/overwrite original csv
0
 
LVL 67

Author Closing Comment

by:sirbounty
ID: 39824921
Oh, I thought I'd tried that and it didn't work...
It seems to have this time, however.  Thanks
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824980
Well, it sort of works...

Any idea why it's doing this?
$CustInfo = @(Import-Csv agedata.csv)
$newCust = newData "Ted" "33"

$combineData = $CustInfo + $newCust
$combineData | sort custname | export-csv agedata.csv -notype

Open in new window


then the file contains:
"custname","custage"
"",
"John","23"
"Tom","41"


I don't know where that extra blank line is coming from...
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824994
What's weirder still is it doesn't seem to do it every time... :(
0
 
LVL 40

Expert Comment

by:footech
ID: 39825209
I can't replicate what you're seeing.  Sounds like an empty line/blank record is getting in there, but I don't see where.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39825308
Fiddled with it and finally got it working...no idea on the problem though.  thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question