Solved

Load Data From Parameter File into Array of Strings

Posted on 2015-01-23
7
116 Views
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)
{
    processes...
}

What is the best way to get a group of individual string values into the array?
0
Comment
Question by:dbbishop
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:footech
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
$PARMS["Msg1"]

Open in new window

0
 
LVL 15

Author Comment

by:dbbishop
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 .\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
 
LVL 15

Author Comment

by:dbbishop
ID: 40567617
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 39

Expert Comment

by:footech
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

0
 
LVL 15

Author Comment

by:dbbishop
ID: 40567662
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
 
LVL 39

Accepted Solution

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

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
 
LVL 15

Author Closing Comment

by:dbbishop
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.
Thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now