2

I'm attempting to use PowerShell to create a hash table where the key is a servername which has multiple values some of which are arrays.

Key = SERVERNAME

Values:

ServerID (Single ID value)

GroupIDs (array of ID values, which tie to a specific group name below)

GroupName (Array of values, which ties to specific group ID from array above)

I'm iterating through a list of servers and attempting to build this out.

$ServerHashTable = @{}
$ServerHashTable.ServerName = @()
$ServerHashTable.ServerID = @()
$ServerHashTable.GroupName = @()
$ServerHashTable.GroupID = @()

$ServerHashTable.ServerName += $ServerName
$ServerHashTable.ServerID += $ServerID
$ServerHashTable.GroupName += $GroupName
$ServerHashTable.GroupID += $GroupID

The issue I have is that the key doesn't appear to be the servername and I'm not sure how to link the GroupID's and GroupNames as I add them.

Thanks for any help with this

2 Answers 2

2

It looks like the data structure you may want to use would be an object rather than a hashtable which is key/value (singular).

$ServerName = 'ExampleServerName'
$ServerID = 12345
$GroupName = @('ExampleGroup1','ExampleGroup2')
$GroupIDs = @(1,2,3)

$Servers = @()
$Servers += [pscustomobject]@{
    ServerName = $ServerName
    ServerID   = $ServerID
    GroupName  = $GroupName
    GroupID    = $GroupID
}

Then you could use Where-Object to filter for name

$Servers | Where-Object {$_.ServerName -eq 'ExampleServerName'}

Note that the [pscustomobject] type accelerator is version 3 or newer. Earlier versions need to use New-Object.

Sign up to request clarification or add additional context in comments.

6 Comments

This worked very well! Thank you. I did get stuck on adding additional GroupNames and GroupID's to the existing array under the given ServerName. I assume I might need to use the where-object to reference the given servername first and I see how to add the values, but I can't seem to get them to work together.
The only other question is, would it be possible to link the GroupID with the GroupName when I add them? or maybe I should just enter them as a single value?
It turns out I just needed to add them while in the current loop.
These seem to be individual columns of data. Is there any way to link the data? Where the servername is the key and the rest of the data are the values? So I can reference the servername to get all the associated values? As an example, when I add a second groupname and group ID, they are listed as the 2nd value in the array. e.g. $Servers[0] includes the servername, serverID, GroupName, GroupIDs and then $Servers[1] includes $groupID, $GroupName
@MrMr These are not individual columns of data. Each object is separate, each with the properties that it is given. So as stated in the Answer, if you want to retrieve objects with a specific piece of information then use Where-Object to filter. I think your issue is that you are creating an object per group rather than one per server with an array of groups as a property. As for "linking" You could put GroupName and GroupID inside another nested pscustomobject.
|
1

I think you are misunderstanding how hashtables work. In your case, ServerName, ServerID, GroupName and GroupID are all keys. Each has an array as the corresponding value.

It's not clear exactly what you're trying to do, but a collection of custom objects might be more appropriate. How you'd construct them will depend how you are gathering / looping though the data, but in general, you can do something like this:

$servers = @()

$servers += [PsCustomObject]@{
                ServerName = $serverName
                ServerID =$serverID
                GroupName = $groupName
                GroupID = $groupID
            }

1 Comment

This worked very well! Thank you. I did get stuck on adding additional GroupNames and GroupID's to the existing array under the given ServerName. I assume I might need to use the where-object to reference the given servername first and I see how to add the values, but I can't seem to get them to work together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.