How to parse .ini file into PowerShell variables

I have the following .ini file (named metrics.ini) containing one record, but more records may be added in the future:

$DatabaseConnection_STR=MYSERVER\MYINSTANCE

I need to parse this file in a PowerShell variable. I can parse the string as follows, but I find it difficult to create a new $ DatabaseConnection_STR variable (based on what was parsed from the .ini file). I don't want the hardcode $ DatabaseConnection_STR in my script - I would rather let the script understand that it can handle additional variables in the future.

# This code assumes that no blank lines are in the file--a blank line will cause an early termination of the read loop

    ########################################
    #
    # Confirm that the file exists on disk
    #
    ########################################

    $IniFile_NME="C:\temp\metrics.ini"

    dir $IniFile_NME

    ########################################
    #
    # Parse the file
    #
    ########################################

    $InputFile = [System.IO.File]::OpenText("$IniFile_NME")

    while($InputRecord = $InputFile.ReadLine())
        {
            # Display the current record

            write-host "`$InputRecord=$InputRecord"
            write-host ""

            # Determine the position of the equal sign (=)

            $Pos = $InputRecord.IndexOf('=')
            write-host "`$Pos=$Pos"

            # Determine the length of the record

            $Len = $InputRecord.Length
            write-host "`$Len=$Len"

            # Parse the record

            $Variable_NME = $InputRecord.Substring(0, $Pos)
            $VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1)

            write-host "`$Variable_NME=$Variable_NME"
            write-host "`$VariableValue_STR=$VariableValue_STR"

            # Create a new variable based on the parsed information--**the next line fails**

            `$Variable_NME=$VariableValue_STR
        }
    $InputFile.Close()

Any ideas?

+3
source share
2 answers

It may be simpler and easier to use the split command. You can also save your configuration values ​​in a hash table:

$config = @{}

Get-Content $IniFile_NME | foreach {
    $line = $_.split("=")
    $config.($line[0]) = $line[1]
}

, -, , Powershell .

+6

. , New-Variable ($) "-name"; . . .

# This code assumes that no blank lines are in the file--a blank line will cause an early termination of the read loop

########################################
#
# Confirm that the file exists on disk
#
########################################

$IniFile_NME="C:\temp\metrics.ini"

dir $IniFile_NME

########################################
#
# Parse the file
#
########################################

$InputFile = [System.IO.File]::OpenText("$IniFile_NME")

while($InputRecord = $InputFile.ReadLine())
    {
        # Display the current record

        write-host "`$InputRecord=$InputRecord"
        write-host ""

        # Determine the position of the equal sign (=)

        $Pos = $InputRecord.IndexOf('=')
        write-host "`$Pos=$Pos"

        # Determine the length of the record

        $Len = $InputRecord.Length
        write-host "`$Len=$Len"

        # Parse the record

        $Variable_NME = $InputRecord.Substring(1, $Pos -1)
        $VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1)

        write-host "`$Variable_NME=$Variable_NME"
        write-host "`$VariableValue_STR=$VariableValue_STR"

        # Create a new variable based on the parsed information

        new-variable -name $Variable_NME -value $VariableValue_STR
        get-variable -name $Variable_NME
    }
$InputFile.Close()
0

All Articles