Wednesday, 25 May 2016

How to work with INI file using powershell



Hello,

In my previous post "How to parse an INI file in powershell", I explained how to parse an Ini file and extract its content. In this article, I’ll show you how to exploit the output of this function.
A quick reminder:

The function parseInifFile receives an *ini file as input and returns an array. The array contains two columns which looks like this:

Section                        Content
-------                           -------
[owner]                       {name,organization}
[database]                   {server,port,file}


The first column contains all Sections of the Ini file (type: String). And the second column contains the pair key/value (type: Hashtable).

With this little introduction, let's go to the next step:

You can wether use your own ini file or take the file "my_INI_FILE.ini" available here.
Open powershell and browse to the directory containing the file. Copy the content of common.ps1 and execute it so that we can use the function parseIniFile, or execute the function parseIniFile in your current PS session. Then enter the following command line:

Now you should have something like this:








First of all, you need to identify what are you looking for in your INI file, then you can proceed. I selected some scenarios that may be useful to you in the second part of this article.

Basic manipulation:

If you want to access a value manually with nothing special in mind; this is how it’s done:

Select the hashtable $myIniContent  and redirect the output; that way we can access to the property Section and/or Content like it’s shown below:












In powershell v4.0, you can access it directly by doing
$my_INI_Content.Section if you want all sections
$my_INI_Content.Content if you want all contents

You can access a specific Section and its Content by specifying his index between brackets. Note that the index start form 0 not 1; so count carefully J . For instance, if you want to access the second line you need to type 1, not 2. 
Here is an example:
I want to access the first line of  $my_INI_Content, I just need to type $my_INI_Content[0].


Now, you know the entire trick to exploit the output of the function.

Going further:

That’s it for the easy task. But what if we want to go further? 
Here begins the difficult part:

Case 1:

       Let's suppose that a specific section exists (but you don’t know its index) and you want to access its content:
Create the variable $SectionToFind to specify which section you're looking for like below.


As you can see in the picture, to access key/value content, you just need to type $TargetSection.Content .


Case 2:

Let’s do something even more complicated. In our INI file, we know that the key “server” exists, and we want to find to which section it belongs and what is its value.

So we want something like this:
  - Key entered: "server" -
The section is : [database]
The value is : 192.168.2.62

How do we achieve that?

Well, it's not really an easy task. I even spent a lot of time on it.

But before that, here is the first approach for people who don't need the section in their output (I understand that you may want to save many lines of code):



It works also with duplicated key in your ini file (at the condition that it’s not duplicated inside the same Section).


However, if you want the value of Section, you need to insert an intermediate value inside the pipeline. So it becomes something like this:


And that gives you this output:




I wrapped all of those scripts inside a tool that list all sections and content (keys/values) if you are interested. Here is a preview of the tool:


You can download it here.

That’s all folks!
Thanks for reading !
Share:

0 commentaires:

Post a comment

Popular Posts

Join us on Facebook

STATS

Contact form

Name

Email *

Message *