Jose
Jose

Reputation: 11

powershell - add array to existing nested hash table

I have an array or PSCustomObject called: $results = @()

I have a nested hash table called: $allResults = @{}

$results |ft, shows this in console:

Brokering        Name             WriteCacheSize   dFreeSpace   (and more...)   
---------        ----             --------------   ----------            
N/A              server0001       0,004            22,83             
N/A              server0002       0,004            21,86           

$allresults |ft shows this:

Name                       Value                                                
----                       ----
server0001                     {SCOMStatus, PVSServer, RegistrationState, ServerLoad...}
server0001                     {SCOMStatus, PVSServer, RegistrationState, ServerLoad...}                     

The name is in common in both $. There should be a way to add the values in $results to $allresults, but I don't know how to do it.

I want everything in the hash nested table. How can I add the values in the $results to the $allresults. The goal is having a nested table with every value.

Upvotes: 1

Views: 10293

Answers (2)

Jose
Jose

Reputation: 11

Thanks everybody for their responses. I finally achieved to do it. I created another object with custom properties, and then I fullfilled the hash table. This is the code:

$myObjectProperties = @(
"Brokering"
"PVSServer"
"Name"
"VDA"
"WMI"
"vDisk"
"dFreeSpace"
"WriteCacheSize"
"Spooler"
"CitrixPrint"
"Uptime"
"cFreeSpace"
"RunspaceId"
)
foreach ($element in $results){
    foreach ($property in $myObjectProperties){
        $allresults.$($element.Name).$property = $element.$property
    }
}

Upvotes: 0

Jason Snell
Jason Snell

Reputation: 1465

If you're trying to add the values of an array to a hashtable you can loop through the array and use $allresults.Add(<key>,<value>).

Upvotes: 0

Related Questions