none
Need help in automating the scanning/comparing web.config files thoroughly RRS feed

  • General discussion

  • Good morning!

    I'm a very beginner on PS scripting and trying to learn it these days. I've got a very recent project requirements to automate the web.config file comparison (just like how we usually do by using some tools like 'BeyondCompare' etc). I've tried to test out some scripting commands but unable to achieve the desired result. Here's what my scripting code is:

    # This PowerShell script will find which app settings keys are missing from one config. file or the other

    [xml]$file1 = Get-Content "C:\Temp\Web.DEV.config"
    [xml]$file2 = Get-Content "C:\Temp\Web.PROD.config"
    Compare-Object ($file1.SelectNodes("//add[@key]") | Select-Object -ExpandProperty Key) ($file2.SelectNodes("//add[@key]")  | Select-Object -ExpandProperty Key)

    Output:

    InputObject          SideIndicator
    -----------          -------------
    nonoamtestuserid     =>           

    I got an idea of how that indicator on the 'SideIndicator' column works but still unable to achieve the desired results as that commands just scans and finds for the missing app setting keys in either of those files. 

    Here's what the exact requirements are:

    The script should scan the whole "Web.config" file from the Development server (downloaded locally) with the "Web.config" file from the Production server (also downloaded locally) and compare them in a way that it should identify and display any differences in it (not just with app settings differences) and should prompt the developers to resolve those differences.  In an event of having no differences found, it should simply prompt to say something like 'Approve and proceed'. Resolving the differences I think should be done manually by the developers but the script needs to notify whether to approve any of those differences or to review them separately.

    I'd be really thankful for your precious time for helping me out! :)

    Thanks again,

    Amy


    Aamee

    Thursday, June 20, 2019 2:51 PM

All replies

  • I could imagine that you get more / faster / better results in the apporpriate forums:

    https://forums.iis.net/


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, June 20, 2019 3:35 PM
  • Please first read the initial post in the forum:

    This forum is for scripting questions rather than script requests

    My recommendation is to break the problem down into very small and discrete pieces. We can help if you have questions on those, but (as noted in the link) it's not the purpose of this forum to write code for you based on a specification.


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 20, 2019 4:05 PM
    Moderator
  • XML files are notoriously hard to compare.  You must loop through one files hierarchy and search the second file for a matching node.  The rules of what is a match are highly dependent on what you consider different/same.

    This is a programming challenge even for experienced coders.  Start by learning how to enumerate the nodes in an XML file.


    \_(ツ)_/

    Thursday, June 20, 2019 4:35 PM
  • Oh ok I see... I'll explore more first into enumerating the nodes in XML files first then.... I know this task is pretty tough for a beginner like me :(...

    Thanks for your help though.



    Aamee

    Thursday, June 20, 2019 5:37 PM
  • Here is a starter:

    [xml]$dev = Get-Content C:\Temp\Web.DEV.config
    $dev.DocumentElement.ChildNodes |
        ForEach-Object{$_.Name}

    Just simple enumeration of node.  Designing code to get each node layer and find if there is a match in a second XML is the challenge.

    Here is a resource that will help:

    https://www.w3schools.com/xml/default.asp

    Once you understand XML and the DOM the PS object makes the rest easy.  The hard part is designing a simple recursive enumerator that does what you ask.

    Or you could just download an XML comparison utility like this one: https://www.altova.com/xml-diff-a?gclid=CjwKCAjw3azoBRAXEiwA-_64Or0yBXPNSSp8hYOa7iPsSAP7VuuRJOYYolJqArCXaegSa9_Kd65gbBoCewoQAvD_BwE


    \_(ツ)_/

    Thursday, June 20, 2019 6:24 PM
  • Thanks, that helps. That starter script helps me to understand how to get the names of each top level XML nodes from a single file. But is it also possible to retrieve all other attributes from that $dev object variable? Like more info other than just 'Name' only?

    Actually, we're already using different tools which help us in finding the xml differences but it's all manual. So that's why looking to automate this tedious process via the script. 

    I'll explore more those resources which you've provided and will go from there.

    Thanks!


    Aamee

    Thursday, June 20, 2019 7:13 PM
  • Thanks, that helps. That starter script helps me to understand how to get the names of each top level XML nodes from a single file. But is it also possible to retrieve all other attributes from that $dev object variable? Like more info other than just 'Name' only?

    Actually, we're already using different tools which help us in finding the xml differences but it's all manual. So that's why looking to automate this tedious process via the script. 

    I'll explore more those resources which you've provided and will go from there.

    Thanks!


    Aamee

    Absolutely.  You can get anything you need but first you have to learn PowerShell.

    The program from the link I posted does al of this and does not require any manual intervention.  The trial is free.


    \_(ツ)_/

    Thursday, June 20, 2019 7:43 PM
  • That utility looks good but I forgot to mention earlier that this file comparison process needs to be automated via our build process (CI) build. We're using TeamCity as our primary build automation tool so probably once this process gets automated through a script then maybe we can just add a PowerShell step in our build to execute that script on a build agent/server.




    Aamee

    Thursday, June 20, 2019 8:46 PM
  • Why would you need to do this comparison in a build?  That doesn't make much sense.  A Web.config file is a very special entity in that is set limits and specifies resources.  These are all stored in a database each time the web loads.  No one should be changing this.  What changes are you trying to find in a build?

    It sounds to me like you have a web management and deployment issue that needs to be addressed first.

    The only other thing I can think of is for you to hire a consultant who knows XML and IIS.Net.

    You can also post in the IIS forum to see if they have any tools that may do what you need.

    You biggest issue is that you are trying to say how you want to do something without saying why.  You need to have a reason first.  The reason helps to dictate the tools needed to address the need.  This is just basic engineering.

    The link to the comparer likely has a command line version that can be run in batch. Have you explored that?


    \_(ツ)_/

    Thursday, June 20, 2019 8:55 PM
  • Yes, need to address the deployment issues. The idea is to get the files thoroughly compared before it gets deployed on any lower or higher environments. The config files have some environment specific configuration entries which needs to be validated each time before it gets deployed on any environments like DEV/QA/Prod etc.

    I couldn't find the command line version from that utility. I made sure to go with the default Install options. Also explored all the menu options too.


    Aamee

    Thursday, June 20, 2019 9:17 PM
  • If this is critical you will need to hire a consultant.  To code this can take a couple of days depending on the full set of requirements.

    I have done this before with other XML for a few thousand documents.  It was fairly complex set of requirements and took three days to just get to the testing phase.  It took another two days to tweak the code to work smoothly under all circumstances.  Unfortunately the code is all in C# and would not be of any use if you are not a pretty good C# programmer.


    \_(ツ)_/

    Thursday, June 20, 2019 9:33 PM
  • Here is the command line version of DiffDog: https://www.altova.com/diffdog-server

    \_(ツ)_/

    Thursday, June 20, 2019 9:36 PM