In one of my scripts, i noticed that when i store a custom object in one array, and then, if i modify the object properties, all changes are made in the array too.
Is there a simple way to store objects by value?
I want to avoid recreating a new object each time i want to store its value.
Example:
PS D:\wamp\www> $obj = New-Module -ScriptBlock { $var1="value1"; Export-ModuleMember -Variable * } -AsCustomObject
PS D:\wamp\www> $arr = @()
PS D:\wamp\www> $arr += $obj
PS D:\wamp\www> $arr
var1
----
value1
PS D:\wamp\www> $obj.var1 = "newvalue"
PS D:\wamp\www> $arr += $obj
PS D:\wamp\www> $arr
var1
----
newvalue
newvalue
PS D:\wamp\www> $obj2 = $obj.Psobject.Copy()
PS D:\wamp\www> $obj2.var1 = "other"
PS D:\wamp\www> $arr += $obj2
PS D:\wamp\www> $arr
var1
----
other
other