locked
Compare XML Values on Element Match RRS feed

  • Question

  • Having issues figuring this one out on my own.  I need to compare 2 xml files say a baseline.xml and a compare.xml using PowerShell.  Both of these files will have a list of file names using full path and a file hash.  I need to get one object, FilePath, from the compare.xml and search the baseline then compare the hash, if there is a difference not this in an array for later use.  Below are the XML files examples.

    Baseline.xml

    <FileChecks>
        <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
        <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6" />
        <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
        <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB" />
    </FileChecks>

    Compare.xml

    <FileChecks>
        <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
        <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6_DIFFERENT" />
        <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
        <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB_DIFFERENT" />
    </FileChecks>

    Any examples would be helpful.

    Thanks



    Friday, February 3, 2017 3:26 AM

Answers

  • [xml]$xml1 = @'
    <FileChecks>
    <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
    <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6" />
    <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
    <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB" />
    </FileChecks>
    '@
    [xml]$xml2 = @'
    <FileChecks>
        <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
        <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6_DIFFERENT" />
        <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
        <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB_DIFFERENT" />
    </FileChecks>
    '@
    
    Compare-Object $xml1.FileChecks.File $xml2.FileChecks.File -IncludeEqual -PassThru -Property FilePath,Hash
    


    \_(ツ)_/


    • Edited by jrv Friday, February 3, 2017 4:26 AM
    • Marked as answer by Bill_Stewart Monday, March 6, 2017 7:34 PM
    Friday, February 3, 2017 4:24 AM

All replies

  • What have you tried.  It seems simple enough to just load the XML and compare the "File" elements.  Have you tried that?


    \_(ツ)_/

    Friday, February 3, 2017 3:47 AM
  • [xml]$xml1 = @'
    <FileChecks>
    <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
    <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6" />
    <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
    <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB" />
    </FileChecks>
    '@
    [xml]$xml2 = @'
    <FileChecks>
        <!--Created: 2/2/2017 8:47 PM on AJBDEV-PRIME-->
        <File FilePath="D:\test\bfsvc.exe" Hash="BE379B732A8A9C15A0F5D99933A695CEE6F4382D30975DB9D48D4FE4F9E73530FFC88C285692F6BDF5BF0F1EBE90A9ECF68AA40228A27CB1059461C555B697C6_DIFFERENT" />
        <File FilePath="D:\test\explorer.exe" Hash="877AC7A9DD81E53A994F233BFAB9EB828287E7C89B5DC424FC12DEB3C777ACCE7916627220B44F5E0F3A0C07DCC8319E7A843462865C0431DEEE79B58FAF9101" />
        <File FilePath="D:\test\HelpPane.exe" Hash="ECC3E15EA6AC2C02535BE78F525F192776FF18AC7D70E441C220290C58668C8FD9B4422ADC75B0FAFD8114E11D301BC7DF2D96B2C718D6464B1F53C7894268AB_DIFFERENT" />
    </FileChecks>
    '@
    
    Compare-Object $xml1.FileChecks.File $xml2.FileChecks.File -IncludeEqual -PassThru -Property FilePath,Hash
    


    \_(ツ)_/


    • Edited by jrv Friday, February 3, 2017 4:26 AM
    • Marked as answer by Bill_Stewart Monday, March 6, 2017 7:34 PM
    Friday, February 3, 2017 4:24 AM
  • Sorry been busy with work all morning. That more than meets my requirements.  I was trying to go in and match up the FilePaths and then see if there were differences in the Hashes.  This not only will allow me to see differences but also if an entire file is missing or added.

    Thank You very much!

    Friday, February 3, 2017 6:54 PM