Click here to Skip to main content
15,888,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an INI file, look like this :

[Setting]
Level=5
Name=x:\process.log
Log=1
Type=2

[State]
stage=10
step=Capture
status=BEGIN
Step=
Status=


I want to read specific section of the INI file. The way I want to read that element is like this :
I run it with CMD :

Script.ps1 Get-Infile -pathofINIfile Setting Name Setting_Name.cmd

I want to read section [Setting] and value of "Name" , then write it in to file "Setting_Name.cmd"

What I have tried:

Function Get-IniFile
{

Param(
  [parameter(mandatory=$true)][string]$path
)
$inifile = $path
$ini = @{}

Get-Content $inifile | ForEach-Object {
  $_.Trim()
} | Where-Object {
  $_ -notmatch '^(;|$)'
} | ForEach-Object {
  if ($_ -match '^\[.*\]$') {
    $section = $_ -replace '\[|\]'
    $ini[$section] = @{}
  } else {
    $key, $value = $_ -split '\s*=\s*', 2
    $ini[$section][$key] = $value
  }
}

$read = $ini.Setting.Name
$read

}
Posted
Updated 21-Mar-19 21:54pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900