Saturday, October 30, 2021

Automated Check for Compliant INI Files

 

So, one of the things that's a fact of life is that we use INI files for configuration of some of our software, and comparing these files, or in my case ensuring certain values are present. It's been a long-term goal that I find a way (build a tool) to do the heavy lifting.

This is the result (right)...

At the point this screen capture was taken the output was to screen alone. I've added in a text-based report and an html format report too at work, a challenge in itself. The real trick with this is that I use a template, an INI file, to configure what is matched or compared for the outcome report as shown.

Using the template as the configuration tool for this means that the user has the control and they can customize based on their requirements.

This was my original plan, to offer matching (m:), compare (c:), and information (i:) but I dropped the information option as compare seems to serve the purpose. I haven't found a serious need for case sensitivity. That may change.

For now the features have been set and the coding is done. Okay, done is a strong word I can already think of a few tweaks. I am the captain of Scope Creep. Software is never done, it's just released.   :-)

The key to this script was arrays. Okay, arrays and hash tables. The first task though was to load each of the found files into hash tables. To do this I prepended the [section] headers to the keys as listed and filtered out the blanks, comments and non-compliant lines:

Parsed it looks more like this:

Which cleans things up and makes it usable as a hash table. 

 

There's another array I capture, the list of all of the unique section.key sets from all of the INI files so I know what all of the available options are. Technically this also captures the section.key sets from the template file too so if the set I'm looking for is missing from all files I'll have it in the list.

I read the template file into a hash table for compliance and an array for comparison. The template as interpreted looks like this:

To generate the report I will spin through the hash table of the template settings looking and for compliance comparing each entry, per file, to the master. This worked well to display the match success (Y/N) in columns. That was tossed out when I got to the comparisons. The other thing I tossed out was matching whole sections by specifying the m: designation on a [section].

The Command Line:

I added switches to enable filtering/specifying what files we use for the INI sources (Customer/Environment) and for which template to use considering different teams at work will have different needs. It will use a default template.ini if not specified, which is also customizable.

vini.ps1 -CustID <xxxx> -EnvID <test> [-template test.ini]

 Oh, there's one more:

[-interactive] - which displays all the "work" that went into the results. Which is mostly what I display above.

Validate INIs: vINI - My Cousin vINI?

How would you write this script? Don't tell me, just think about it. The methods I used I considered for this took some time, as well as how to present the report. Sure, text was the first step so I had data could validate, but an on-screen output has limited play. I used Powershell but there are other options of course. Generating the data on screen I took the time to use colour so it was easy to read. The text file doesn't use that of course, but the HTML file uses colour and CSS styling with colour. 

The last thing I did was to send an email to the person executing the script, the text file and the html file are attached, but the email format uses the same HTML styling for continuity and readability. Sorry, I don't have an example handy to add here.

No comments:

There is no individual ownership when you are part of a team, it's the sum of the parts that makes you the RESILIENT team you need to be.