I need to write a PowerShell script to edit an .ini file. I am not able to use Get-IniContent as it is not available by default. I am using the below function, which works well if there is no duplicate property names in different sections. But the .ini file I am editing does have a property name that is in multiple sections. How would I modify the code to take section into consideration?
function Set-OrAddIniValue
{
Param(
[string]$FilePath,
[hashtable]$keyValueList
)
$content = Get-Content $FilePath
$keyValueList.GetEnumerator() | ForEach-Object {
if ($content -match "$($_.Key)\s*=")
{
$content= $content -replace "$($_.Key)\s*=(.*)", "$($_.Key)=$($_.Value)"
}
else
{
$content += "$($_.Key)=$($_.Value)"
}
}
$content | Set-Content $FilePath
}
#Set-OrAddIniValue -FilePath $configFile -keyValueList @{"name"=$name}