March 22, 2011 at 10:02 am
I have the following .ini file (named metrics.ini) containing a single record, but it may have more records added to it in the future:
$DatabaseConnection_STR=MYSERVER\MYINSTANCE
I need to parse this file into a PowerShell variable. I can parse the string with the following, but I am at a loss for creating the new $DatabaseConnection_STR variable (based on what was parsed from the .ini file). I don't want to hardcode $DatabaseConnection_STR in my script--I would rather let the script figure it out so that is can handle additional variables in the future.
# 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 = $InputRecord.IndexOf('=')
write-host "`$Pos=$Pos"
# Determine the length of the record
$Len = $InputRecord.Length
write-host "`$Len=$Len"
# Parse the record
$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"
# Create a new variable based on the parsed information--the next line fails
`$Variable_NME=$VariableValue_STR
}
$InputFile.Close()
Any ideas?
March 22, 2011 at 10:50 am
This works. It turns out that the New-Variable command does not use a dollar sign ($) with its "-name" parameter; so, I had to parse that out. See below.
# 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 = $InputRecord.IndexOf('=')
write-host "`$Pos=$Pos"
# Determine the length of the record
$Len = $InputRecord.Length
write-host "`$Len=$Len"
# Parse the record
$Variable_NME = $InputRecord.Substring(1, $Pos -1)
$VariableValue_STR = $InputRecord.Substring($Pos + 1, $Len -$Pos -1)
write-host "`$Variable_NME=$Variable_NME"
write-host "`$VariableValue_STR=$VariableValue_STR"
# Create a new variable based on the parsed information
new-variable -name $Variable_NME -value $VariableValue_STR
get-variable -name $Variable_NME
}
$InputFile.Close()
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy