How to create new clone instance of PSObject object

前端 未结 9 687
野趣味
野趣味 2020-12-09 02:20

I want to create new instance of my custom PSObject. I have a Button object created as PSObject and I want to create new object Button2 which has the same members as Button

9条回答
  •  既然无缘
    2020-12-09 02:59

    Put this in a Utility class or define it in your current section

    function clone($obj)
    {
        $newobj = New-Object PsObject
        $obj.psobject.Properties | % {Add-Member -MemberType NoteProperty -InputObject $newobj -Name $_.Name -Value $_.Value}
        return $newobj
    }
    

    Usage:

    $clonedobj = clone $obj

提交回复
热议问题