Load Data From Parameter File into Array of Strings

Posted on 2015-01-23
Last Modified: 2015-01-23
I am reading data from a parameter file such that I am reading a key and value pair. When finished, one group of values I have in PowerShell is something like:
$STRING1 = GetParameterValue $PARMS "KEY1"  -- $PARMS is an array of key/value pairs
$STRING2 = GetParameterValue $PARMS "KEY2"

What I want to end up with is an array (e.g. $Strings) so that I can do something like:

foreach($s in $Strings)

What is the best way to get a group of individual string values into the array?
Question by:dbbishop
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 40

Expert Comment

ID: 40567608
An array of key/value pairs - that is a hashtable.
Say you have a file called stringdata.txt with the following:
Msg1 = Value1
Msg2 = Value2
Msg3 = Value3

Open in new window

You can read that into a hashtable with the following:
$PARMS = Get-Content stringdata.txt | Out-String | ConvertFrom-StringData

Open in new window

You can then reference a value associated with a key like

Open in new window

LVL 15

Author Comment

ID: 40567615
Current code segment is:
function GetParameterValue($ParmFile, $ParmName)
	$tmp = $ParmFile | where { $_.Key -eq $ParmName } | Select Value
	[string]$result = $tmp.value
	return $result.trim()

$PARMS = import-csv .\

$Value = GetParameterValue $PARMS "Key" 

Open in new window

I am reading about 15 values but only 10 of them I want to be put into a string array. So assume that key/value pairs 1..5 are standalone values used elsewhere and 6..15 are to be read into the string array so they can be parsed using the foreach syntax. The other five values are used for other purposes. I have the code already in place to read all the values, now I just want to get the 10 into an array of string values. I don't want to rewrite the parsing code because it is pretty much standard and used in a number of places.
LVL 15

Author Comment

ID: 40567617
The file contains the key value pairs is in the format of:
but that should be irrelevant because, like I said, I already have all the parsing code for it in place, the intent is to get value6..value15 into an array of strings.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Expert Comment

ID: 40567648
I'm having trouble understanding your use case and what variations may occur.
Here's a shot though.
You can collect all the values into an array like this.
$array = "key1","key2","key7" | ForEach { GetParameterValue $PARMS $_ }

Open in new window

You can use Select-Object to get the first or last X values, or skip X values.
$array | Select -Last 10

Open in new window

LVL 15

Author Comment

ID: 40567662
I have 10 distinct variables with values:

I want to get the values of those 10 variables into an array. The code I provided above is pretty much a template of how we read a config file of properties, so I do not want to vary from that (how the file is processed and the values are parsed. What I want to do is get the values of 10 variables into a single array of strings so I can use foreach()

Look at it as though how these 10 variables came to be or how their values were obtained is irrelevant. The use case is to get the values of 10 variables into an array of strings. And to make it simple so that 10 can become 5 or 20 somewhat easily. I used $string1..$string10 as examples, but they might as well be $myvara, $somethingelse, $bubbles, $grapes, $ten, $whoareyou, $idontknow, $down, $left, $gone. Those are 10 variables whose values I want to put into a single string array, the same way I would build $array = @("one", "two", "three"...)
LVL 40

Accepted Solution

footech earned 500 total points
ID: 40567704
It does matter how the variables came to be or else you're left with no option but manually adding each variable to the array explicitly.
Whether you do
$Value1 = GetParameterValue $PARMS "Key1"
$Value2 = GetParameterValue $PARMS "Key2"
$Value3 = GetParameterValue $PARMS "Key3"
$array = $Value1,$Value2,$Value3

$array = @()
$array += $Value1 = GetParameterValue $PARMS "Key1"
$array += $cheese = GetParameterValue $PARMS "Key2"
$array += $burger = GetParameterValue $PARMS "Key3"

$array = "key1","key2","key3" | ForEach { GetParameterValue $PARMS $_ }
doesn't matter.  The last way is just a condensed version.

If you've already got $Value1, $Value2, and $Value3 (or whatever they're named) and don't want to modify your code that you've shown above, then the only way to put them in an array is like below, explicitly adding each by name.
$array = $Value1,$Value2,$Value3

For this to be expandable to many variables, you either have to have a record of all the variable names, or have some similarity in how they are named so that you could run a command like
$array = Get-Variable String* | Select -expand Value

If possible, it's better to collect all the values into an array before you assign individual pieces to variables.
LVL 15

Author Closing Comment

ID: 40567744
I will likely use $array = $Value1,$Value2,$Value3, but I like the idea of: $array = "key1","key2","key3" | ForEach { GetParameterValue $PARMS $_ }.
Although the process of reading the parameter file is set, I can use the normal method to read the values not associated with the array and use the second method to read into the array. Either would be easy to maintain. I am sure the resources involved in either method would be similar.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question