PoshCode Archive  Artifact [a53bc86026]

Artifact a53bc86026fca160f21e28ff46fdc31c41e34594db2079b2953510965f4ac93e:

  • File new-pshash.ps1 — part of check-in [dd3ef45e69] at 2018-06-10 13:11:54 on branch trunk — this function takes nested hashtables and converts them to nested pscustomobjects.it can also contain arrays of hashtables, and it will turn those hashtables in the arrays also into PScustomobjects (user: unknown size: 2259)

# encoding: ascii
# api: powershell
# title: new-pshash
# description: this function takes nested hashtables and converts them to nested pscustomobjects.it can also contain arrays of hashtables, and it will turn those hashtables in the arrays also into PScustomobjects
# version: 0.1
# type: function
# license: CC0
# function: new-pshash
# x-poshcode-id: 2522
# x-archived: 2011-02-28T08:08:23
#
#
#this function takes nested hashtables and converts them to nested pscustomobjects
#it can also contain arrays of hashtables, and it will turn those hashtables in the arrays
#also into PScustomobjects
function new-pshash 
{
[CmdletBinding()]
PARAM(
[parameter(valuefromPipeline=$true,Mandatory=$true, position = 1)]
[HashTable]$fromHashTable,
[Parameter(position = 2)]
[String]$typename

)
process { 
    
    $obj = new-object pscustomobject -Property $fromhashtable
    if ($typename) { $obj.psobject.typenames.insert(0,$typename) } 
    foreach($prop in $obj.psobject.properties)
    {
     if ($prop.value -is [hashtable])
        {            
            $obj.$($prop.name) = new-pshash $($obj.$($prop.name))            
        } else
        {                  
         if ($prop.value -is [Object[]])
            {                
                $k2 = $obj.($prop.name).count
                for($index = 0; $index -lt $obj.($prop.name).count; $index++)
                {                                                    
                  if ($obj.($prop.name)[$index] -is [hashtable])
                   {         
                      $obj.$($prop.name)[$index] =  new-pshash $obj.$($prop.name)[$index]                                              
                   }                
                }
            }
        }       
    }
    $obj
 }

}

#test object, hash of hashes of arrays of hashes
$testobject = @{
 top1 = 1
 #nested of nested
 nested = @{ nest = 'two'
             deepernest = @{ deepest = 1; whoohoo = 2 }
 }
 simplearray = ( 1 , 2 , 3 )
 hasharray = (
              @{ name = 'karl'
                 age = '30' },
              @{ name = 'john'
                 age = '40' }
             )
}

 
 
 $a = new-pshash $testobject karlclass
 $a