[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

working with csv objects in powershell

Posted on 2014-01-31
10
Medium Priority
?
991 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 41

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 41

Accepted Solution

by:
footech earned 1000 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 41

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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