When I create scripts I always try to make them as flexible/re-usable as possible. That always means many parameters that can be changed in the script. Usually, there are two ways how you can customize/change script parameters:
– hardcode them at the first lines of your code (it is never a good idea)
– or pass them as parameters during script execution (if you have many parameters it is unreadable)
But there is the other, very simple, way to pass many parameters to your script. Create a simple configuration (.ini) file that will store all all parameters that can be changed. The biggest advantage of the .ini file is clarity of all parameters, you can easily create section for your parameters, and even add comments to describe how to use them.
I created a simple function that load the .ini file:
Function loadConfig ($fileName, $oneSection) {
$config = Get-Content ($fileName)
$cfg = @{}
$sec = $cfg
$section = ''
ForEach ($line in $config) {
$line = $line.Trim()
if ($line.startswith('[')) {
$section = $line.replace('[','').replace(']','')
if ($oneSection -eq '*') {
$cfg.Add($section,@{})
$sec = $cfg.$section
}
continue
}
if ($oneSection -ne $null -and $section -ne $oneSection -and $oneSection -ne '*') {continue}
$k = $line.IndexOf('=')
if ($line -eq '' -or $k -lt 1 -or $line[0] -in '[','#') {continue}
$sec.Add($line.Substring(0,$k), $line.Substring($k+1))
}
Return $cfg
}
The function returns a hashtable with all parameters specified in .ini file. There are only four rules that you have to remember when you create the .ini file:
– there must be “=” (equal sign) after the parameter name
– lines started with “#” will be skipped
– section names are represented like “[section_name]”
– parameter names in one section must be unique
Now let’s prepare a simple .ini file (config.ini
):
#demo config file
min=5
[website section]
min=10
url=http:\mrpanas.com
[other section]
min=15
max=255
data with space=200
The function is prepared to read all sections or only one specific section.
We have a few options how it can be executed:
1. Without the second parameter. In this case, parameter names in the .ini file have to be unique and all will be loaded as a flat structure.
2. We can specify in the second parameter which section do we want to load – empty ( “” ) section name means that only parameters on the main block (without the section will be loaded).
Examples:
$conf = loadConfig "c:\tmp\config.ini" ""
the result of $conf.min
is 5
$conf = loadConfig "c:\tmp\config.ini" "website section"
the result of $conf.min
is 10
$conf = loadConfig "c:\tmp\config.ini" "other section"
the result of $conf.min
is 15
3. The last option is to read all sections in a hierarchical structure. In this case for all sections will be created a hash table that will store parameters from the section. To do this, use “*” as the second parameter of the function.
Example:
$conf = loadConfig "c:\tmp\config.ini" "*"
the result of $conf.min
is 5
the result of $conf."website section".min
is 10
the result of $conf."other section".min
is 15
If you like this post, please share or/and recommend it on Facebook.