-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCopy-Object.ps1
69 lines (61 loc) · 2.06 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function Copy-Object {
<#
.SYNOPSIS
To copy an object to standard output
.DESCRIPTION
To copy an object to standard output. Any complex data structure (beyond int, datetime, double, etc)
that is assigned to another variable that second variable is actually a pointer to the dataset
already in memory. What this allows you to do is to get a true copy where you can manipulate the
second variable without affecting the contents of the first variable
.PARAMETER InputObject
The object that you want copied.
.EXAMPLE
# Example of what is WRONG with assigning a variable object to another variable
$A = New-Object -TypeName 'psobject' -Property ([ordered] @{ Name = 'server' ; Value = 12 })
$B = $A
$B.Value = 3
$A
Name Value
---- -----
server 3
# Which is probably not what you want to do.
.EXAMPLE
$A = New-Object -TypeName 'psobject' -Property ([ordered] @{ Name = 'server' ; Value = 12 })
$B = Copy-Object -InputObject $A
$B.Value = 3
$A
Name Value
---- -----
server 12
#>
# todo Change += to System.Collections.Arraylist
#region Parameter
[CmdletBinding(ConfirmImpact='None')]
[OutputType('psobject')]
Param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[psobject[]] $InputObject
)
#endregion Parameter
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
$result = @()
}
process {
foreach ($curObject in $InputObject) {
if ($curObject.gettype().name -eq 'HashTable') {
$result += $curObject.Clone()
} else {
$tempObject = New-Object -TypeName PsObject
$curObject.psobject.properties | ForEach-Object {
$tempObject | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
$result += $tempObject
}
}
}
end {
Write-Output -InputObject $result
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}