I have the following .ini file (named metrics.ini) containing one record, but more records may be added in the future:
$DatabaseConnection_STR=MYSERVER\MYINSTANCE
I need to parse this file in a PowerShell variable. I can parse the string as follows, but I find it difficult to create a new $ DatabaseConnection_STR variable (based on what was parsed from the .ini file). I don't want the hardcode $ DatabaseConnection_STR in my script - I would rather let the script understand that it can handle additional variables in the future.
$IniFile_NME="C:\temp\metrics.ini"
dir $IniFile_NME
$InputFile = [System.IO.File]::OpenText("$IniFile_NME")
while($InputRecord = $InputFile.ReadLine())
{
write-host "`$InputRecord=$InputRecord"
write-host ""
$Pos = $InputRecord.IndexOf('=')
write-host "`$Pos=$Pos"
$Len = $InputRecord.Length
write-host "`$Len=$Len"
$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"
`$Variable_NME=$VariableValue_STR
}
$InputFile.Close()
Any ideas?
source
share