Egyedi objektumok átalakítása hashtáblává, hashtáblák átalakítása egyedi objektummá (Convert-CustomObjectHash)

 

function Convert-CustomObjectHash {

<#

.Synopsis

   Merges properties / keys of the Secondary object / hashtable to the Primary object / hashtable.

.DESCRIPTION

   This function takes all properties or keys of the Secondary object / hashtable into the Primary object or hashtable. By default only those properties / keys are merge that doesn't exist in the Primary object / hashtable.

   If the -Force switch is used then the properties / keys of the Secondary object / hashtable always merged to the Primary.

.EXAMPLE

    $o = @{ObjProp = [pscustomobject] @{Prop1 = 1; Prop2 = 2}; HashProp = @{Key1 = 1; Key2 = 2}}; $result = Convert-CustomObjectHash -Object $o

 

    Because parameter -To is not specified, the conversion will be from [pscustomobject] to [hashtable], including all properties that are also [pscustomobject].

.EXAMPLE

    $o = @{ObjProp = [pscustomobject] @{Prop1 = 1; Prop2 = 2}; HashProp = @{Key1 = 1; Key2 = 2}}; $result = Convert-CustomObjectHash -Object $o -to pscustomobject

 

    Because the -To parameter is [pscustomobject], only the HashProp of the input object will be converted to [PScustomobject].

.INPUTS

   hashtables or pscustomobjects

.OUTPUTS

   The converted input object

#>

[cmdletbinding()]

param(

    # Object to convert.

    [Parameter(Mandatory = $true, ValueFromPipeline = $true)][AllowNull()] [object]$Object,

    # Force conversion to this datatype.

    [Parameter()] [ValidateSet('hashtable', 'pscustomobject')] [AllowNull()] [string] $To,

    # Recursion depth, by default 5.

    [Parameter()][int] $Depth = 5,

    [Parameter(DontShow = $true)][int] $_currentdepth = 0

)

process{

    if($null -eq $Object){

        return

    }

   

    if($Object -isnot [hashtable] -and $Object -isnot [System.Management.Automation.PSCustomObject]){

        return $Object

    }

 

    if($_currentdepth -gt $Depth){

        return

    }

 

    if(!$PSBoundParameters.to){

        if($Object -is [hashtable]){

            $To = 'pscustomobject'

        }

        elseif($Object -is [System.Management.Automation.PSCustomObject]){

            $To = 'hashtable'

        }

        else{

            return

        }

    }   

 

    if($To -eq 'hashtable'){

        $newObject = @{}

 

        foreach($prop in $Object.psobject.properties){

            $newObject.($prop.name) = Convert-CustomObjectHash -Object $prop.value -To $To -_currentdepth ($_currentdepth + 1) -Depth $Depth

        }

        $newObject

    }

    elseif($To -eq 'pscustomobject'){

        $Object = [pscustomobject] $Object

        foreach($prop in $Object.psobject.properties){

            $Object.($prop.name) = Convert-CustomObjectHash -Object $prop.value -To $To -_currentdepth ($_currentdepth + 1) -Depth $Depth

        }

        $Object

    }

}

}

 

 



Word To HTML Converter