Rajiv
Rajiv

Reputation: 685

capture data from ini file PowerShell

I would like to parse an ini file and capture data from a Section. Basically I am trying to capture the version of an AV installed on 1000+ Servers. the ini file contains "Program_Version". This variable contains the version no. If I search using Program_Version=, I directly find the phrase after the "=" sign it contains the version no. Like 8.0, 10.6 etc

Can some one please guide me on how to achieve this?

Thanks

Upvotes: 1

Views: 3212

Answers (2)

JPBlanc
JPBlanc

Reputation: 72680

Here is the function I use :

function Parse-IniFile
{
  [CmdletBinding()]
  Param
  (
    [Parameter(mandatory=$true,ValueFromPipeline=$true)]
    [Alias("Fichier")]
    [string]$fic
  )

  begin {} 

  Process 
  {
    $ini = @{}
    switch -regex -file $fic 
    {
      "^\[(.+)\]$"
      {
        $section = $matches[1]
        $ini[$section] = @{}
      }
      "(.+)=(.+)" 
      {
        $name,$value = $matches[1..2]
        $ini[$section][$name] = $value
      }
    }

    return $ini
  }

  end {}
}

given an INI file from "C:\Windows\System32\DriverStore\FileRepository" directory tree.

$p = Parse-IniFile "C:\Windows\System32\DriverStore\FileRepository\adihdaud.inf_amd64_neutral_66552f06054bc4ee\Mixer.ini"
$p["FRENCH"]["KSPINNAME_ADI_ALT_PCBEEP_SOURCE"]

gives

"Bip PC"

Upvotes: 1

mjolinor
mjolinor

Reputation: 68331

.ini files are normally key = value pairs, that are easily convertible to a hash table using ConvertFrom-StringData

Read a properties file in powershell

From there it's just a matter of reading the value of the hash Program_Version key from the hash table.

Upvotes: 0

Related Questions