首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将.ini文件解析为PowerShell变量

如何将.ini文件解析为PowerShell变量
EN

Stack Overflow用户
提问于 2011-03-23 00:37:40
回答 2查看 9.5K关注 0票数 4

我有以下名为metrics.ini的.ini文件,其中只包含一条记录,但将来可能会添加更多记录:

代码语言:javascript
复制
$DatabaseConnection_STR=MYSERVER\MYINSTANCE

我需要将这个文件解析成一个PowerShell变量。我可以用以下代码解析字符串,但是我不知道如何创建新的$DatabaseConnection_STR变量(基于从.ini文件解析的内容)。我不想在我的脚本中硬编码$DatabaseConnection_STR --我宁愿让脚本来解决它,这样它就可以在将来处理额外的变量。

代码语言:javascript
复制
# 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()

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-06-15 02:27:19

使用split命令可能更容易、更简洁。您还可以将配置值存储在哈希表中:

代码语言:javascript
复制
$config = @{}

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

如果不需要哈希表,您仍然可以使用相同的方法创建变量,但是使用Powershell的读取、循环和拆分将更容易。

票数 7
EN

Stack Overflow用户

发布于 2011-03-23 00:48:28

这是可行的。事实证明,New-Variable命令没有使用带有"-name“参数的美元符号($);因此,我必须将其解析出来。见下文。

代码语言:javascript
复制
# 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5394681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档