Solved

working with csv objects in powershell

Posted on 2014-01-31
10
902 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

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.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

730 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