小编use*_*601的帖子

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

我有以下包含单个记录的.ini文件(名为metrics.ini),但将来可能会添加更多记录:

$DatabaseConnection_STR=MYSERVER\MYINSTANCE
Run Code Online (Sandbox Code Playgroud)

我需要将此文件解析为PowerShell变量.我可以使用以下内容解析字符串,但是我无法创建新的$ DatabaseConnection_STR变量(基于从.ini文件解析的内容).我不想在我的脚本中硬编码$ DatabaseConnection_STR - 我宁愿让脚本弄清楚它以便将来可以处理其他变量.

# 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 = …
Run Code Online (Sandbox Code Playgroud)

powershell

4
推荐指数
1
解决办法
9270
查看次数

标签 统计

powershell ×1