Working with INI style files in PowerShell


We love PERL at the place where I work. I love it too; it has its own set of advantages. One of the major advantages PERL had was that PowerShell was not around when the solutions were automated. We have the whole SQL*Server installation, configuration and deployment of jobs patted down to putting relevant values in a configuration file and running a PERL script. I wrote parts of the script and love how it turned out.

Anyways, about year ago I was working on moving some parts of our deployment scripts to PowerShell. One of the things that was on the to-do list was parsing and building hashes out of .ini style files. It was not uncommon to find these files used for persisting configuration information and also for automation so, I assumed we would have a built-in cmdlet to cover this kind of a file. We do not have it.

It is a little disappointing but, since the structure of the file is almost static; we can code very easily around it. I had a different issue because our configuration files were semi-structured and had some xml style information as well; we can talk about that later. However, for the .ini style files the basic structure is simple and is setup like a hash where a key has a value i.e. Key = Value.

Once we get the content of the file using Get-Content cmdlet; we can then start parsing the file line by line. The trickiest part for me was getting rid of the comment lines. For example, if we assume ‘#’ is the escape character, we can have it appear anywhere in the line. If the line starts with ‘#’ we can ignore the line. But, if a line is something like ‘Key1 = Value25 #But value25 is not real {o_0}’; we need the key and its corresponding value and need to exclude the comments. Fortunately for us after we get the content of the file; we only need two lines to get this job done for us:

 $CfgFileContent = Get-Content .\sampleconfig.ini

 #Line1: Remove comment Lines, those that start with '#'
 $CfgFileContent = $CfgFileContent -replace "^`#.*$", ""

 #Line2: Remove comments anywhere else on line
 $CfgFileContent = $CfgFileContent -replace "`#.*$", ""

See the below image to get a feel of what is happening in the variable into which we read the file.

changes in ini files as we go through regex operations.

changes in ini files as we go through regex operations.

As you can see we have eliminated the comments from the array (Get-Content; returns the result as an array so to speak). But, we do see that we have an element that has no values in it and yes, that was the comment line that we deleted earlier. So, we need to be mindful of this when we process the array to build our hash.

Now all we have left to-do is to move through each line of the file and build the hash which can be used later on.

foreach ($line in $CfgFileContent)
{
  <#Region-Begin: Process configuration file that is setup like a hash where a key has a value#>
  $lineContent = [regex]::split($line, '=')

  #ignore empty keys
  if(($lineContent[0].CompareTo("") -ne 0) -and (-not($lineContent[1] -eq $null))){
	#work through the config file.
	$lineContent[0] = ($lineContent[0].Trim()).TrimEnd()
	$lineContent[1] = ($lineContent[1].Trim()).TrimEnd() -replace "`#.*$", "" 
	$ConfigValues[$lineContent[0]] = $lineContent[1] 
  }
 <#Region-End: Process configuration file that is setup like a hash where a key has a value #>
}

Kindly, let me know what you think.

Advertisements
About

By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , , ,
Posted in Fun-Stuff, PowerShell
3 comments on “Working with INI style files in PowerShell
  1. jammyjim says:

    Thanks, this is exactly what I was looking for, although it seems the code window hasn’t escaped your double quotes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: