Quantcast
Viewing all articles
Browse latest Browse all 21975

Make a Copy of a 'New-Module -AsCustomObject'

I would be most grateful if somebody could assist in solving this issue for me. I have created a rather complex module, with many properties, methods etc. I create arrays of these objects and have the need to clone one. As the objects are referenced by the array, I am really struggling to get this to work. Here is some sample code:

Function MyModule {
	param(
		[Parameter(Position=0,ValueFromPipeline=$false,Mandatory=$true)][System.String]$MyVariable
		)
	Begin{
		New-Module -AsCustomObject -ArgumentList $MyVariable -ScriptBlock {
		param(
			[Parameter(Position=0,ValueFromPipeline=$false,Mandatory=$true)][System.String]$test
			)
		Export-ModuleMember -Variable test
		}
	}
}

$MyArray = @()
$MyArray += MyModule -MyVariable "Member1"
$MyArray += MyModule -MyVariable "Member2"
$MyArray += MyModule -MyVariable "Member3"
Write-Host "Initial MyArray:"
$MyArray

$MyArray += $MyArray[0]
$MyArray[-1].Test = "Member4"

$MyArray += $MyArray[1].PsObject.Copy()
$MyArray[-1].Test = "Member5"

$MyArray += $MyArray[2].PsObject.Copy()
$MyArray[-1].Test = "Member6"


Write-Host
Write-Host "Duplicated Elements MyArray:"
$MyArray

Here is the output:

Initial MyArray:

test                                                                                                                                                       
----                                                                                                                                                       
Member1                                                                                                                                                    
Member2                                                                                                                                                    
Member3                                                                                                                                                    

Duplicated Elements MyArray:
Member4                                                                                                                                                    
Member5                                                                                                                                                    
Member6                                                                                                                                                    
Member4                                                                                                                                                    
Member5                                                                                                                                                    
Member6                                                                                                                                                    


As you can see, members 4, 5 & 6 are still the same object as what was members 1, 2, 3.

Thank you in advance for any assistance/solutions.

Regards,


Mark Rhoades-Brown



Viewing all articles
Browse latest Browse all 21975

Trending Articles