Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

Load Data From Parameter File into Array of Strings

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)
{
    processes...
}

What is the best way to get a group of individual string values into the array?
0
dbbishop
Asked:
dbbishop
  • 4
  • 3
1 Solution
 
footechCommented:
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
$PARMS["Msg1"]

Open in new window

0
 
dbbishopAuthor Commented:
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 .\CreateDatabase.properties

$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.
0
 
dbbishopAuthor Commented:
The file contains the key value pairs is in the format of:
key1,value1
key2,value2
...
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.
0
NEW Internet Security Report Now Available!

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out this quarters report on the threats that shook the industry in Q4 2017.

 
footechCommented:
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

0
 
dbbishopAuthor Commented:
I have 10 distinct variables with values:
$string1
$string2
$string3
...
$string10

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"...)
0
 
footechCommented:
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

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

or
$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.
0
 
dbbishopAuthor Commented:
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.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now