functions/Copy-Object.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<#
    .NOTES
        There is no PowerShell function to make a deep copy.
        Clone() function for hashtables does only a shallow copy.
        Reference has to be used here to handle null or empty objects.
 
    .DESCRIPTION
        This function writes a deep copy of an input object
        to a reference passed.
        If original object is null, then null will be returned.
        Otherwise object is copied via memory stream.
 
    .PARAMETER Original
        It is an object that will be copied.
        It can be null.
 
    .PARAMETER DeepClone
        It is a reference to an existing object.
        This object will become a deep copy of original object passed to this function.
        [ref] is used as PowerShell returns all non-captures stream output,
        not just argument of the return statement.
        e.g. it will not return empty array, but null instead.
 
    .EXAMPLE
        $clone = $null
        Copy-Object -Original $original -DeepClone ([ref]$clone)
#>

function Copy-Object {

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$false)]
        $Original,

        [Parameter(Mandatory=$true)]
        [ref] $DeepClone
    )

    if($null -eq $Original)
    {
        $DeepClone.Value = $null
    }
    else
    {
        $memStream = new-object IO.MemoryStream
        $formatter = new-object Runtime.Serialization.Formatters.Binary.BinaryFormatter
        $formatter.Serialize($memStream, $Original)
        $memStream.Position = 0
        $DeepClone.Value = $formatter.Deserialize($memStream)
        $memStream.Close()
    }
}