开发者

How to Parse .ini File into PowerShell Variable(s)

I have the following .ini file (named metrics.ini) containing a single record, but it may have more records added to it in the future:

$DatabaseConnection_STR=MYSERVER\MYINSTANCE

I need to parse this file into a PowerShell variable. I can parse the string with the following, but I am at a loss for creating the new $DatabaseConnection_STR variable (based on what was parsed from the .ini file). I don't want to hardcode $DatabaseConnection_STR in my script--I would rather let the script figure it out so that is 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?


It's probably easier and more concise to use the split command. You could also store your configuration values in a hash table:

$config = @{}

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

You could still use the same method of creating variables if don't want a hash table, but using Powershell's reading, looping, and splitting will make it easier.


This works. It turns out that the New-Variable command does not use a dollar sign ($) with its "-name" parameter; so, I had to parse that out. See below.

# 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜