Solved

working with csv objects in powershell

Posted on 2014-01-31
10
859 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
  • 7
  • 3
10 Comments
 
LVL 39

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
 
LVL 39

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 39

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now