Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add a column to default output of export-csv

Posted on 2012-04-10
3
Medium Priority
?
653 Views
Last Modified: 2012-04-12
Is there a way to do add a second column to the CSV in the following code snip?  I'm putting this in a loop that goes through multiple files.  I need another column so I know what file I need to look at when I get a get diff, and I can make it into a table in Excel for easy viewing.

I need the output to be one file, and am going to add Dmitri's funtion from http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/.  

I'm comparing one master set of source files against files from different servers, which is why I need to path as a column.

compare-object -referenceobject $(get-content $sourceFile) -differenceobject $(get-content $serverFile) | export-csv -Path d:\some.csv -notype -append
0
Comment
Question by:1974Widget
3 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 37827331
Well, I would add two columns, to have both reference and target file names:
compare-object (get-content $sourceFile) (get-content $serverFile) |
  Add-Member NoteProperty SourceFile $sourceFile -PassThru|
  Add-Member NoteProperty TargetFile $serverFile -PassThru |
  export-csv -Path d:\some.csv -notype -append

Open in new window

0
 
LVL 6

Expert Comment

by:slidingfox
ID: 37827380
Give the code below a try.


$comparison = compare-object -referenceobject $(get-content $sourceFile) -differenceobject $(get-content $serverFile)

$comparison | % {

    $obj = New-Object -type PSObject
    $obj | Add-Member -membertype NoteProperty -name InputObject -Value $_.InputObject
    $obj | Add-Member -membertype NoteProperty -name SideIndicator -Value $_.SideIndicator
    $obj | Add-Member -membertype NoteProperty -name Path -Value $source

    return $obj
   
}

$obj | export-csv -Path d:\some.csv -notype -append
0
 

Author Closing Comment

by:1974Widget
ID: 37840407
Just what I asked for!  Thanks!
0

Featured Post

Ready for your healthcare security check-up?

In the past few years, healthcare organizations have become a prime target for advanced attacks. Does your organization have what it needs to defend itself? Schedule your healthcare security check-up today and download our free Healthcare Security Resource Kit today!

Question has a verified solution.

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

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

886 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