Storing file information to compare later

This topic contains 8 replies, has 2 voices, and was last updated by  Lery 3 years, 10 months ago.

  • Author
    Posts
  • #12636

    Lery
    Participant

    I'm trying to think of some ideas on how to approach something that I would like to do. I have a XML file that I would like to store in a PowerShell script. I would like to run the script and have it check the XML file on the endpoint and have the script output the differences from the XML stored in the script and the XML file on the endpoint.

    I know I can deploy the original XML file with the script and use that as a source to compare against. I'd like to avoid that method.

    I'm looking for ideas on how to do this?

  • #12647

    Lery
    Participant

    Hi Dave. In your example I do not see here-string. I'll have to look that up, but your idea looks promising. Am I just copying and pasting my XML into the script as in your example?

  • #12660

    Dave Wyatt
    Moderator

    The Here-string is surrounded by @' and '@, in this case. (Double-quoted versions also work, if you wanted to be able to expand PowerShell variables and expressions inside it.) Refer to the about_Quoting_Rules help file for information on this topic.

    But basically, yes, you would just copy and paste XML code into the script.

  • #12664

    Lery
    Participant

    Thanks again Dave. I'm surprised that this was so simple. The output is something to be desired, but it works. Anything else I could use other than compare-object? What I'm doing is something like this:

    $xml = @'
    < ?xml version="1.0"?>


    Gambardella, Matthew
    XML Developer's Guide
    Computer 44.95 2000-10-01 An in-depth look at creating applications
    with XML.



    '@

    compare-object -ReferenceObject $xml -DifferenceObject $(get-content c:\file.xml)

    My file.xml for reference is something like this:

    < ?xml version="1.0"?>


    ThisIs, Different
    XML Developer's Guide with some changes
    Computer 44.95 2000-10-01 An in-depth look at creating applications
    with XML.


    You can see the author tag and the title tag are different. I would like to have my output say something like:

    Author value is different. Author value equals: ThisIs, Different
    Title value is different. Title value equals: XML Developer's Guide with some changes

  • #12665

    Dave Wyatt
    Moderator

    You basically have two approaches here; you can compare text (which might be awkward if identical nodes are simply in a different order between files), or you can convert those to objects and write some sort of function to search for differences. It's going to be somewhat tricky, but it's certainly doable.

    I think the first step would be to decide on the requirements. Using the two XML snippets you posted, what would you want PowerShell to produce as output describing the differences?

  • #12668

    Lery
    Participant

    I will be calling the here-string the "source of truth" I will need to read in a file which will have different values or possibly have added lines. The output from the compare-object does the job, but I certainly would like to have better output. In a perfect world I would love to have it output the default value, located in the here-string, and the value that is currently in the XML file that I read in. If the values are equal, then output nothing. If the values are different simply have something that I gave in my example above.

  • #12669

    Dave Wyatt
    Moderator

    I guess I see three types of differences that can exist between XML files, off the top of my head:

    • Elements (or trees of elements) that exist in one file but not the other.
    • Elements that exist in both files, but have different attributes (or the same attributes with different values; possibly two categories of difference here.)
    • Elements that exist in both files, with different inner text data.

    The code to check for these types of differences in the tree is not too bad (it'll probably involve some sort of recursive function to walk the tree of elements in both files), but you'd have to decide how best to represent these differences in the output. In particular, what if an entire tree of XML elements is missing from one file?

    Maybe for your purposes, you don't need it to be quite this generic, and just want to check the elements / attributes that are defined in your authoritative XML code. That's easier to write, and you can simply report on whether the authoritative elements / attributes exist in the file you're reading, and if their values are the same.

  • #12671

    Lery
    Participant

    To start, I think it would be easier to take the path of least resistance. So for now, I'm going to go for just telling me if the XML I'm reading is different from my authoritative source. If it is different, output the differences.

    Now I had to check the actual file, which I cannot post because it's loaded with confidential data, is not truly an XML file. It's is .config file with hints of XML formatting inside it. Using my trusty XML Notepad viewer, I can see the file basically has a node, followed by another node which is the same throughout the file. Each of these nodes has three nodes. Those are the values I'm reading. I can output the current file into out-gridview and it's pretty. It would be nice if I could compare the two files and output the differences into out-gridview all nice and pretty. I'll work on that.

    Any better ideas you have, please share.

  • #12638

    Dave Wyatt
    Moderator

    Saving the original XML inside your script file is easy; just use a here-string, like this:

    $xml = @'
    < ?xml version="1.0"?>
    
       
          Gambardella, Matthew
          XML Developer's Guide
          Computer
          44.95
          2000-10-01
          An in-depth look at creating applications 
          with XML.
       
    
    '@
    

    Comparing the files might be a little trickier. A quick web search turned up this StackOverflow post, which looks promising: http://stackoverflow.com/questions/13223765/compare-xml-structure-using-powershell

You must be logged in to reply to this topic.