Objektumok tulajdonságainak változtatása (Update-Property)

 

function Update-Property {

param(

    [psobject] $object,

    [string]   $propname,

    [psobject] $value = 1,

    [switch]   $passthru,

    [switch]   $force

)

    if($null -eq $object){

        Write-Error "No object - update propery"       

        return

    }

    if($object -is [hashtable] -and !$object.containskey($propname)){

        $object.$propname = $value

    }

    elseif($object -isnot [hashtable] -and $object.psobject.Properties.Name -notcontains $propname){

        Add-Member -InputObject $object -MemberType NoteProperty -Name $propname -Value $value

    }

    elseif($force){

        $object.$propname = $value

    }

    elseif($object.$propname -is [int] -and $value -is [int]){

        $object.$propname += $value

    }

    elseif($object.$propname -is [string]){

        if($value -ne $object.$propname){

            $object.$propname = @($object.$propname) + $value

        }

    }

    elseif($object.$propname -is [collections.ilist]){

        if($object.$propname -is [collections.ilist] -and $object.$propname.count -gt 0 -and $object.$propname[0] -is [hashtable]){

            if($value -is [collections.ilist] -and $value.count -gt 0 -and $value[0] -is [hashtable]){

                $existingKeys = $object.$propname | ForEach-Object {$_.Keys}

 

                if($existingKeys -notcontains ($value.keys | Select-Object -First 1)){

                    $object.$propname += $value

                }

                else{                   

                    $equalfound = $false

                    foreach($v in $object.$propname){

                        $difffound = $false

                        foreach($k in $v.keys){

                            if($v.$k -ne $value[0].$k){

                                $difffound = $true

                                break

                            }

                        }

                        if(!$difffound){

                            $equalfound = $true

                            break

                        }

                    }

                   

                    if(!$equalfound){

                        $object.$propname += $value

                    }

                }

            }

        }

        else{

            foreach($v in $value){

                if($object.$propname -notcontains $v){

                    $object.$propname += $v

                }

            }

        }

    }

    elseif($object.$propname -is [System.Collections.Hashtable] -and $value -is [System.Collections.Hashtable]){

        $keys = [object[]] $value.keys

        foreach($key in $keys){

            $object.$propname.$key = $value.$key

        }

    }

    else{

        $object.$propname = @($object.$propname) + $value

    }

 

    if($passthru){

        $object

    }

}



Word To HTML Converter