D B
asked on
Parse Config File Values into an Array
I have been tasked with removing as much hard-coded data as I can from a script and put it into a configuration file to be parsed by the script. I've got two challenges, both very similar but with differences.
My configuration file will have a key/value pair such as:
emailTo,me@here.com,you@th ere.net,hi m@somewher e.org
I've used this method quite a bit in the past, but now, some of the values need to be parsed into an array to meet the requirements of the cmdlet (e.g. Send-MailMessage-multiple addresses has to be an array of string). So the challenge is to have a parameter with the name emailTo go into a variable in PoSh named $emailTo with the value of @("me@here.com","you@there .net","him @somewhere .org")
What I am doing now, and would like to try to keep the same 'format' is I read the file in with: $CSV = import-csv .\my.prop, then use the following function call:
$var = GetParameterValue $CSV "VAR"
where GetParameterValue is coded thusly:
I would prefer NOT to have to put the quotes around each address, but will if it will help.
The second issue is more challenging, I need to pass a parameter to SQLCMD along with the query script, with has a SQLCMD variable in it. The variable basically defines a list of database names to EXCLUDE when querying master.sys.sysdatabases. The code is:
name NOT IN ($(DB_EXCLUSION_LIST))
After playing with it for 1/2 a day, I finally learned that I need to pass parameters as an array in name=value format. Thus, instead of:
$exclusions = "'DB1', 'DB2','DB3'"
SQLCMD.EXE -E -S $server -y 2048 -h -1 -d MainDB -i $sqlScript -o $serverOutfile -s "|" -v DB_EXCLUSION_LIST = $exclusions
it turned out to be
$dbExclusionList = @("DB_EXCLUSION_LIST=`"'DB 1','DB2',' DB3'`"")
SQLCMD.EXE -E -S $server -y 2048 -h -1 -d MainDB -i $sqlScript -o $serverOutfile -s "|" -v $dbExclusionList
Not sure how I am going to do that in a config file. Ideally, again, I'd have DB1,DB2,DB3 and any formatting would be done in my script, but then again I never have gotten everything I wanted. If someone can figure out a clean way to save the value in the config file and parse it into an array, I would sure appreciate it.
My configuration file will have a key/value pair such as:
emailTo,me@here.com,you@th
I've used this method quite a bit in the past, but now, some of the values need to be parsed into an array to meet the requirements of the cmdlet (e.g. Send-MailMessage-multiple addresses has to be an array of string). So the challenge is to have a parameter with the name emailTo go into a variable in PoSh named $emailTo with the value of @("me@here.com","you@there
What I am doing now, and would like to try to keep the same 'format' is I read the file in with: $CSV = import-csv .\my.prop, then use the following function call:
$var = GetParameterValue $CSV "VAR"
where GetParameterValue is coded thusly:
function GetParameterValue($ParmFile, $ParmName)
{
$tmp = $ParmFile | where { $_.Key -eq $ParmName } | Select Value
[string]$result = $tmp.value
return $result.trim()
}
To simplify things, I would like to work with the string, and make the determination in code if it can be left as a string, or parsed into an array based on the variable name.I would prefer NOT to have to put the quotes around each address, but will if it will help.
The second issue is more challenging, I need to pass a parameter to SQLCMD along with the query script, with has a SQLCMD variable in it. The variable basically defines a list of database names to EXCLUDE when querying master.sys.sysdatabases. The code is:
name NOT IN ($(DB_EXCLUSION_LIST))
After playing with it for 1/2 a day, I finally learned that I need to pass parameters as an array in name=value format. Thus, instead of:
$exclusions = "'DB1', 'DB2','DB3'"
SQLCMD.EXE -E -S $server -y 2048 -h -1 -d MainDB -i $sqlScript -o $serverOutfile -s "|" -v DB_EXCLUSION_LIST = $exclusions
it turned out to be
$dbExclusionList = @("DB_EXCLUSION_LIST=`"'DB
SQLCMD.EXE -E -S $server -y 2048 -h -1 -d MainDB -i $sqlScript -o $serverOutfile -s "|" -v $dbExclusionList
Not sure how I am going to do that in a config file. Ideally, again, I'd have DB1,DB2,DB3 and any formatting would be done in my script, but then again I never have gotten everything I wanted. If someone can figure out a clean way to save the value in the config file and parse it into an array, I would sure appreciate it.
ASKER
It IS required. My first thought was to code it as a string and it did not work. Research indicates it has to be an array with the each element being a variable=value pair.
an array with the each element being a variable=value pairLike a hash table?
Instead of importing your file as a CSV, why not parse it and put it in a hash table?
Assume it is comma separated, something like this:
$ht1 = @{}
Get-Content .\my.prop | % {
$len = ($_.Split(',')[0]).length
$ht1.Add($_.Substring(0,$len),$_.Substring($len+1))
}
And as far as the SQLCMD, I think footech was saying that if this is just an array with one entry:$dbExclusionList = @("DB_EXCLUSION_LIST=`"'DB1','DB2','DB3'`"")
So this should work too:$dbExclusionList = "DB_EXCLUSION_LIST=`"'DB1','DB2','DB3'`""
But to construct it from your key file, if the key for you DBs is "DBexclusions", you could do something like this:$dbExclusionList = "DB_EXCLUSION_LIST="""+$ht1.DBexclusions
One error in my code, line 4 should be
return $tmp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
None of the answers provided a solution. I had time to do some research online and was able to find workable solutions to my problem.
Open in new window
Your function could beOpen in new window
I'm wondering if the array notation, @(), is really needed in
$dbExclusionList = @("DB_EXCLUSION_LIST=`"'DB
since it's just a string.
Not sure if I can help with SQLCMD issue.