Comparing 2 csv different csv files

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Papa Papa 1 year, 6 months ago.

  • Author
    Posts
  • #32152
    Profile photo of Papa
    Papa
    Participant

    1st CSV file
    ApplicationName,C:DriveSpace,D:DriveSpace,ProcessorCount,CoreCount,BaseMemory,BaseOU,ApplicationVersion,PatchList,ARCHITECTURE
    SCOMLAB,49,100,1,2,8192,PriyaLAB,7.1,a,b,c,64-bit

    2nd CSV file
    ServerName,MODEL,OS,ARCHITECTURE,RAMinMB,CPU,SPEEDinGHz,PHYSICAL,CORE,Drive1,Drive2
    Priya.testlab.com,VMware Virtual Platform,Microsoft Windows Server 2012 R2 Standard,64-bit,2767,Intel64 ,2.4,8,1,C:49.51,D:100.00

    I need to compare the RAMinMB of 2nd csv file with the first csv file BaseMemory, if there is any change then write the present value in 2nd csv file and all the data of 2nd csv file to a 3rd csv file.

    below is my sample code which I am trying to do through. Nothing seems to be getting captured.
    ###########Script #########
    $BaseCSV = import-csv -Path "D:\2nd.csv"
    $NewCSV = import-csv -Path "D:\1st.csv"

    $output = @()
    forEach ($Column in $BaseCsv) {
    $result = $NewCSV | Where-Object {$Column.ARCHITECTURE -eq $_.ARCHITECTURE}
    $OldARCHITECTUREvalue = $result.ARCHITECTURE

    $output += New-object PSObject -property @{

    ServerName = $Column.ServerName
    Model = $Column.Model
    OS= $Column.OS
    Architecture= $Architecture

    }

    }
    $output | select-object SERVERNAME,MODEL,SERIAL,OS,ARCHITECTURE| Export-Csv -Path "D:\TestCompare\change.csv" -NoTypeInformation -Force

  • #32161
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    Are you trying to replace the system architecture or RAMinMB. You stated you were looking to compare the respective Memory columns in your two CSVs, but your code only compares Architecture. Additionally, can you confirm the CSV format of the 1st CSV? Right now your 1st CSV format would result like this:

    ApplicationName : SCOMLAB
    C:DriveSpace : 49
    D:DriveSpace : 100
    ProcessorCount : 1
    CoreCount : 2
    BaseMemory : 8192
    BaseOU : PriyaLAB
    ApplicationVersion : 7.1
    PatchList : a
    ARCHITECTURE : b

    Because the Architecture in your first CSV is = b, your code is not finding a match in $result = $NewCSV | Where-Object {$column.ARCHITECTURE -eq $_.ARCHITECTURE}. So $result is empty.

    You may also want to check out the Join-Object function (http://blogs.msdn.com/b/powershell/archive/2012/07/13/join-object.aspx)
    Thanks,
    Chris

  • #32162
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    Sorry, I also just noticed you are setting ARCHITECTURE value to $Architecture which is never defined so is also empty. We can provide pointers on how to remedy these, but we will need to understand what exactly you are trying to accomplish.

    Thanks,
    Chris

  • #32164
    Profile photo of Papa
    Papa
    Participant

    Sorry , My bad.
    I sorted it out. The data in the csv file was not correct. The data a,b,c has to be a single value. So i just twigged the csv input. And now I am successfully measure the objects.

    ApplicationName,C:DriveSpace,D:DriveSpace,ProcessorCount,CoreCount,BaseMemory,BaseOU,ApplicationVersion,PatchList,ARCHITECTURE
    SCOMLAB,49,100,1,2,8192,PriyaLAB,7.1,a;b;c,64-bit.

You must be logged in to reply to this topic.