PowerShell to Read single value from simple .ini file

大憨熊 提交于 2019-12-05 08:24:35

This looks like a good use case for ConvertFrom-StringData which by default looks for key value pairs separated by the equals symbol.

Because the first line of your .ini file doesn't have an equals we would need to skip it to avoid an error. This can be done simply with Select -Skip 1.

Here's the code:

$ADAP = Get-Content 'ADAP.ini' | Select -Skip 1 | ConvertFrom-StringData

You can then get the values of APP and DLL by accessing them as named properties of the $ADAP object, as follows:

$ADAP.APP
$ADAP.DLL

You can quite easily write a PowerShell function that allows you to read ini files:

function Get-IniFile 
{  
    param(  
        [parameter(Mandatory = $true)] [string] $filePath  
    )  

    $anonymous = "NoSection"

    $ini = @{}  
    switch -regex -file $filePath  
    {  
        "^\[(.+)\]$" # Section  
        {  
            $section = $matches[1]  
            $ini[$section] = @{}  
            $CommentCount = 0  
        }  

        "^(;.*)$" # Comment  
        {  
            if (!($section))  
            {  
                $section = $anonymous  
                $ini[$section] = @{}  
            }  
            $value = $matches[1]  
            $CommentCount = $CommentCount + 1  
            $name = "Comment" + $CommentCount  
            $ini[$section][$name] = $value  
        }   

        "(.+?)\s*=\s*(.*)" # Key  
        {  
            if (!($section))  
            {  
                $section = $anonymous  
                $ini[$section] = @{}  
            }  
            $name,$value = $matches[1..2]  
            $ini[$section][$name] = $value  
        }  
    }  

    return $ini  
}  

$iniFile = Get-IniFile .\ADAP.ini
$app = $iniFile.NoSection.APP
$dll = $iniFile.NoSection.DLL

For this sample ini file saved as Test.ini:

; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.

[database]
; use IP address in case network name resolution is not working
server=192.0.2.62     
port=143
file="payroll.dat"

Doing this:

$testIni = Get-IniFile .\Test.ini

Allows you to retrieve values like this:

$server = $testIni.database.server
$organization = $testIni.owner.organization

You do "just need to read a text file" .. and then find which line begins with APP and then extract the value from it.

# read text file                        # find line beginning APP=
$AppLine = Get-Content -Path test.ini | Where-Object { $_ -match 'APP=' }

# split on = symbol and take second item
$AppVersion = $AppLine.Split('=')[1]

And you might benefit from [version]$AppVersion to make it into a proper sortable, comparable version number instead of a string.

And there's plenty of variation of ways you could do the reading and matching and extracting of a value (Get-Content, switch -file, Select-String, ForEach-Object, -match 'APP=(.*)', etc. in various combinations).

But Mark Wragg's answer is nicer, overall.

$content = Get-Content ADAP.ini

$app = $content[1].Substring($content[1].IndexOf("=") + 1)
$dll = $content[2].Substring($content[2].IndexOf("=") + 1)

You can get the content by calling cmdlet Get-Content and assigning it to variable. By accessing to rows like indexes in arrays you can call methods for working with strings.

note: the code is ugly, I know.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!