Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

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@there.net,him@somewhere.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:
function GetParameterValue($ParmFile, $ParmName)
{
	$tmp = $ParmFile | where { $_.Key -eq $ParmName } | Select Value
	[string]$result = $tmp.value
	return $result.trim()
}

Open in new window

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=`"'DB1','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.
Avatar of footech
footech
Flag of United States of America image

When reading from a file, everything is strings.  So if you need to get an array out of that, your best bet is probably to split a string using some delimiter that you've included.  So if you have a line like
emailTo,me@here.com|you@there.net|him@somewhere.org

Open in new window

Your function could be
function GetParameterValue($ParmFile, $ParmName)
{
	$tmp = $ParmFile | where { $_.Key -eq $ParmName } | ForEach { $_.Value -split "\|" } | ForEach { $_.trim() }
	return $result
}

Open in new window



I'm wondering if the array notation, @(), is really needed in
$dbExclusionList = @("DB_EXCLUSION_LIST=`"'DB1','DB2','DB3'`"")
since it's just a string.
Not sure if I can help with SQLCMD issue.
Avatar of D B

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.
Avatar of Jeremy Weisinger
Jeremy Weisinger

an array with the each element being a variable=value pair
Like 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))
    }

Open in new window

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'`"")

Open in new window

So this should work too:
$dbExclusionList = "DB_EXCLUSION_LIST=`"'DB1','DB2','DB3'`""

Open in new window

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

Open in new window

One error in my code, line 4 should be
	return $tmp

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of D B
D B
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

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.