PowerCLI

 View Only
  • 1.  Merge two csv files both with the same columns

    Posted Nov 28, 2012 09:52 PM

    Hi all,

    I have two csv files (attached) both with the same DS column headers that I am looking to merge into one csv file. I'm struggling to find a way to do this.

    Both files are already sorted by DS but have additional info not required in the DS column. What I need is a cmdlet that will read each DS column from each csv, match the rows and combine into one csv file.

    Any help, guidance, much appreciated.

    Best,

    Rob.



  • 2.  RE: Merge two csv files both with the same columns

    Posted Nov 28, 2012 10:05 PM


  • 3.  RE: Merge two csv files both with the same columns

    Posted Nov 28, 2012 10:16 PM

    Hi Luc,

    I get a blank file as an output. Each csv file's row needs to be linked by the DS object.



  • 4.  RE: Merge two csv files both with the same columns

    Posted Nov 29, 2012 06:09 AM

    I placed both CSV files in a folder C:\TestMerge and executed the following.

    That worked for me

    Get-ChildItem C:\TestMerge\*.csv | 
       ForEach-Object {
         Import-Csv $_ -UseCulture  } |
    Export-Csv C:\TestMerge\MergedCsvFiles.csv -NoTypeInformation  -UseCulture  


  • 5.  RE: Merge two csv files both with the same columns

    Posted Nov 29, 2012 03:09 PM

    Hi Luc,

    It didnt add the LUNUUIDs from one of the input csv files. Here's what I am after:

    From one file I have:

    "DS","VM"
    "L222_0_VM001","VI-SVCS-VM001"

    From the second I have:

    "LunUuid","DS"
    "020000000060060e80102ae0300511a18b000000de444636303046","L222_0_VM001"

    I want to merge the csv files, by the "DS" column header and get this output:

    "LunUuid","DS","VM"
    "020000000060060e80102ae0300511a18b000000de444636303046","L222_0_VM001","VI-SVCS-VM001"

    So that the duplicate "L222_0_VM001" is removed. I need the DS entries to line up with the DS entreies in the other csv first though. The script needs to read, match and remove duplicates. I'm wondering if the LUNUUID might need to be converted to a string. Any ideas?

    Best,

    Rob.



  • 6.  RE: Merge two csv files both with the same columns

    Posted Nov 29, 2012 03:18 PM

    Then you need to go for Joel's  JoinCollections script.

    The example is pretty clear, let me know if you have questions regarding the use of the function ?



  • 7.  RE: Merge two csv files both with the same columns

    Posted Nov 29, 2012 03:36 PM

    Hi Luc,

    This looks like what I need. Just having trouble running it.

    In my case, do I need to edit the script to work with my csv files and, in addition, how would I execute it? I'm thinking:

    .\whatisaveditas $myfirstcsv.csv DS $mysecondcsv.csv | ft -auto



  • 8.  RE: Merge two csv files both with the same columns
    Best Answer

    Posted Nov 29, 2012 05:57 PM

    Save the script in the current folder as Join-Collections.ps1, then do

    $csv1 = Import-Csv LunUUIDtoDS.csv -UseCulture
    $csv2 = Import-Csv VMtoDS.csv -UseCulture
    .\Join-Collections.ps1
    $csv1 "DS" $csv2 | Export-Csv mergedCSV.csv -NoTypeInformation -UseCulture


  • 9.  RE: Merge two csv files both with the same columns

    Posted Nov 29, 2012 07:02 PM

    You are a freaking genius! :smileyhappy:

    You have helped me and my company out in a huge number of ways. Thank you Thank you Thank you!