Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell hashtable with multiple values and one key

Im looking for a data structure/cmdlet that will allow me to add multiple values to a single key in Powershell.

My data would ideally look like this:

KEY-------------------------- VALUES

HOSTNAME1-------------DATABASE1,DATABASE2,DATABASE3

HOSTNAME2-------------DATABASE1,DATABASE2

etc...

I thought a hashtable would do the trick, but I'm unable to do the following:

$servObjects = @{}
$servObjects.Add("server1", @())
$servObjects.get_item("server1") += "database1"

This yields an empty array when I try:

$servObjects.get_item("server1")

I have also tried to do the following, hoping that powershell would understand what I want:

$servObjects2 = @{}
$servObjects2.add($servername, $databasename)

This will unfortunately yield a duplicate key exception

Thanks for any and all input

like image 835
Kevin Price Avatar asked Sep 25 '17 19:09

Kevin Price


People also ask

Can a hash table have multiple values?

Hash tables only have pairs of values, a key and a corresponding item. I see two solutions.

How do I get key value pairs in PowerShell?

The key/value pairs might appear in a different order each time that you display them. Although you cannot sort a hash table, you can use the GetEnumerator method of hash tables to enumerate the keys and values, and then use the Sort-Object cmdlet to sort the enumerated values for display.

Can a hash table have the same key?

If you try to insert a key into a hash table, the usual case should be that there is no key with the same hash in the table yet.

How do I add values to a Hashtable in PowerShell?

You can also use the Hashtable method called Add() to add the value. The format is as below. To remove the Key-value from the Hashtable, you need to use the Remove(Key) method.


2 Answers

If you have your data in an array you can group the array and convert to a hash table:

$ary = @()
$ary = $ary + [PSCustomObject]@{RowNumber = 1; EmployeeId = 1; Value = 1 }
$ary = $ary + [PSCustomObject]@{RowNumber = 2; EmployeeId = 1; Value = 2 }
$ary = $ary + [PSCustomObject]@{RowNumber = 3; EmployeeId = 2; Value = 3 }
$ary = $ary + [PSCustomObject]@{RowNumber = 4; EmployeeId = 2; Value = 4 }
$ary = $ary + [PSCustomObject]@{RowNumber = 5; EmployeeId = 3; Value = 5 }

$ht = $ary | Group-Object -Property EmployeeId -AsHashTable

$ht is then:

Name Value
---- -----
3    {@{RowNumber=5; EmployeeId=3; Value=5}}
2    {@{RowNumber=3; EmployeeId=2; Value=3}, @{RowNumber=4; EmployeeId=2; Value=4}}
1    {@{RowNumber=1; EmployeeId=1; Value=1}, @{RowNumber=2; EmployeeId=1; Value=2}}
like image 199
mortenma71 Avatar answered Sep 24 '22 15:09

mortenma71


In your original example, instead of writing

$servObjects.get_item("server1") += "database1"

you had written

$servObjects.server1 += "database1"

it would have worked.

I'm very new to PowerShell, but I prefer to use

$servObjects.Add("key",@())

over

$servObjects.key = @())

because the .Add will throw a duplicate exception if the key is already present in the hashtable, whereas the assignment will replace an existing entry with a new one. For my purposes, I have found that the implicit replacement is (more often than not) an error, either in my logic, or an anomaly in the input data that needs to be handled.

like image 29
ifdefmoose Avatar answered Sep 24 '22 15:09

ifdefmoose