Reading INI files with powershell

Reading INI files with powershell

I was horrified to learn recently while working on a script that despite Powershell having default commandlets for handling nearly everything from CSV to JSON file formats that there were no options available for reading in the venerable INI format for processing! In order to correct this great injustice I made one:

function import_main_config([String]$strIniPath) {
  if (Test-Path $strIniPath) {
    $hshIniContents = @{}

    switch -regex -file $strIniPath {
      "^[(.+)]$" {
        $strHeading = $Matches[1]
        $hshIniContents[$strHeading] = @{}
      }
      "(.+?)s*=s*(S*)" {
        $strKey = $Matches[1]
        $strValue = $Matches[2]
        $hshIniContents[$strHeading][$strKey] = $strValue
      }
    }
  } else {
    Write-Host "Unable to find INI file at path: $strIniPath"
  }

  return $hshIniContents
}

Now this is very rudimentary, it only does simple Key-Value-Pairs and it doesn’t much care for comments… but it is effective so hopefully this helps.

VMWare To Nagios Plugin Upgrade

This script has been surprisingly popular, unfortunately it’s also the one script I spent half an afternoon throwing together… the time has come however for me to begin looking at giving this nifty little script a facelift. Here’s a list of features I expect to make an appearance in v2.0:

  • Will be written in Powershell so that it can be natively executed on Windows without the need for installing a Perl interpreter.
  • Customizable output strings and mapping options.
  • Better logging.
  • Enhanced stability.

If you use this plugin and there’s any features you would like to see, by all means put it in the comments or drop me an email.

comments powered by Disqus