Home > Net >  Not getting value from nested hash table
Not getting value from nested hash table

Time:01-17

I am not getting "starttime" value below, any idea what I am missing ? The below snippet is part of big script. When I run "$_" I do get correct key value pair but not able to get value based on key name. What is the best method to iterate through hash table ?

$clusterstats = @{
    server1 = @{
                         'NPC Netapp napsdccp138v_prod'= @{
                                                              'nolongerneeded' =  ''
                                                              'status' =  'kRunning'
                                                              'starttime' =  '01/14/2023 08=00PM'
                                                              'clients' =  {

                                                                          }
                                                          }
                         'napsdccp500v_CIFS_new_prod'= @{
                                                            nolongerneeded =  ''
                                                            status =  'kRunning'
                                                            starttime =  '01/13/2023 07=00PM'
                                                            clients =  {

                                                                        }
                                                        }
                         'NPC Netapp napsdccp137v_prod'= @{
                                                              nolongerneeded =  ''
                                                              'status'=  'null'
                                                              'starttime'=  '01/11/2023 09=00PM'
                                                              'clients'=  @{
                                                                              'wfs_prod_home'=  99
                                                                          }
                                                          }
                         'NPC Netapp napsdccp500v_prod'= @{
                                                              'nolongerneeded'=  ''
                                                              'status'=  'kRunning'
                                                              'starttime'=  '01/13/2023 10=00PM'
                                                              'clients'=  {

                                                                          }
                                                          }
                         'NPC Netapp napsdccp352v_prod'=  @{
                                                              'nolongerneeded'=  ''
                                                              'status'=  'kRunning'
                                                              'starttime'=  '01/14/2023 07=00PM'
                                                              'clients'=  {

                                                                          }
                                                          }
                     }


    server2 = @{
                        'NPC Netapp napsdccp353v_prod'=  @{
                                                              'nolongerneeded'=  ''
                                                              'status'=  'kRunning'
                                                              'starttime'=  '01/15/2023 07=00PM'
                                                              'clients'=  {

                                                                          }
                                                          }
                }
    server3 = @{


               }
}

                $clusterstats.GetEnumerator()|sort-object -Property {$_.key} |foreach {  
                                    $server = $_.key
                                    $jobs = @() 
                                foreach ($job in $_.value.keys){
                                                $jobs  = $job
                
                                                        }                                                      
                                if ($jobs.count -gt 0){
                                foreach ($job in $jobs){
     
                                $clusterstats.$($server).$($job).GetEnumerator()|ForEach-Object {
                                           $starttime = $_.value.starttime
                                           $starttime 
                                                                                               }

                                                       }
                                                         }
                                                         }

why $starttime is not showing ??

CodePudding user response:

I think this might be a bit complicated structure you have but this should help you get started:

foreach($server in $clusterstats.GetEnumerator()) {
    foreach($service in $server.Value.GetEnumerator()) {
        if($service.Value.ContainsKey('starttime')) {
            [pscustomobject]@{
                Server    = $server.Key
                Service   = $service.Key
                StartTime = $service.Value['starttime']
            }
        }
    }
}

Using the example data the output would be:

Server  Service                      StartTime
------  -------                      ---------
server1 napsdccp500v_CIFS_new_prod   01/13/2023 07=00PM
server1 NPC Netapp napsdccp352v_prod 01/14/2023 07=00PM
server1 NPC Netapp napsdccp137v_prod 01/11/2023 09=00PM
server1 NPC Netapp napsdccp500v_prod 01/13/2023 10=00PM
server1 NPC Netapp napsdccp138v_prod 01/14/2023 08=00PM
server2 NPC Netapp napsdccp353v_prod 01/15/2023 07=00PM

Unless you have a real need to use a hashtable of hashtables, I don't think this is leveraging the good qualities of IDictionary since you need to enumerate all key value pairs and in that sense this task might be simplified by simply using PSCustomObject.

  • Related