VMware Cloud Community
dreampost
Contributor
Contributor
Jump to solution

Async clone trough script

Can cloning of existing image be done trough some script? If yes can someone please explain?

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can specify the target data store via $VMCloneSpec.location.datastore.

You will have to provide a MORef to the target data store.

The line you would need to add:

$VMCloneSpec.Location.datastore = (Get-View (Get-Datastore -Name <name of target data store>).ID).MoRef


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
11 Replies
halr9000
Commander
Commander
Jump to solution

Sorta/yes and yes.

New-VM has a -RunAsync parameter which does what you want. But that's cloning an existing VM. However, if you convert that VM to a template, then you could specify the template as another parameter to New-VM.

OTOH, if you grab the managed object on a VM (using Get-View or Find-EntityView), there's a method called CloneVM_Task which will do exactly what you want. I've never used it though, I can't at this point tell you exactly how it works beyond what the member definition says:

130# Find-EntityView -ViewType virtualmachine | gm CloneVM_Task | fl


TypeName   : VMware.Vim.VirtualMachine
Name       : CloneVM_Task
MemberType : Method
Definition : VMware.Vim.ManagedObjectReference CloneVM_Task(ManagedObjectReference folder, String name, Vi
             rtualMachineCloneSpec spec)

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
dreampost
Contributor
Contributor
Jump to solution

Sorry, I might have misunderstood this but can the New-VM used to clone an existing VM? That's exactly what I want.

I have a sysprep-ed Windows 2003 Server, would like to multiply it somehow in async mode if possible.

Can you please give me two examples here of cloning, one using the template and other without?

Thanks a lot in advance.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The two cloning examples

1) from a template

New-VM -Name <new-guestname) -Template (Get-Template -Name <template-name>) -VMHost (Get-VMHost -Name <VMhost-name>)

2) from a guest

This is, as far as I know, not foreseen in the current VI Toolkit release.

Unless you first convert the guest to a template via the New-Template cmdlet.

As a bypass you can use the CloneVM_Task method (described in the SDK, see http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.VirtualMachine.html#cl...

$targetfolder = get-view (get-folder -Name Test).ID
$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$VMCloneSpec.powerOn = $FALSE
(Get-View (Get-VM -Name PC2).ID).CloneVM_Task($targetfolder.MoRef, "PC6", $VMCloneSpec)

Note that this will clone an existing guest with the absolute minimum of customisation.

Via $VMCloneSpec.customization one could specify quit some customisation parameters (see http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.vm.customization.Speci...


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dreampost
Contributor
Contributor
Jump to solution

I have one more question though.

In the command syntax you have mentioned:

$targetfolder = get-view (get-folder -Name Test).ID

$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec

$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$VMCloneSpec.powerOn = $FALSE

(Get-View (Get-VM -Name PC2).ID).CloneVM_Task($targetfolder.MoRef, "PC6", $VMCloneSpec)

what is the "Test" parameter for the get-folder cmdlet?

Also I suppose PC2 is the machine to be cloned and PC6 is the clone name.

Thanks in advance.

PS: for others if you will read this topic and use it, the VMware.Vim assembly will have to be loaded first so an extra line has to be added at the begining:

http://Reflection.Assembly::LoadWithPartialName("vmware.vim")

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry for the confusion.

"test" is the name of the target folder in which the new VM guest will be created.

"PC2" is indeed the source VM guest and "PC6" is the target VM guest.

Watch out for the VMware.Vim assembly load command.

The forum SW seems to change the included command.

See for the correct syntax.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dreampost
Contributor
Contributor
Jump to solution

Meanwhile I also figured it out. Smiley Happy

This is the final script:

{Reflection.Assembly}::LoadWithPartialName("vmware.vim")

Get-VIServer servername -user username -password password

$targetfolder = get-view (get-folder vm).ID

$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec

$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$VMCloneSpec.powerOn = $FALSE

(Get-View (Get-VM -Name PCCore).ID).CloneVM_Task($targetfolder.MoRef, "PCClone1", $VMCloneSpec)

(Get-View (Get-VM -Name PCCore).ID).CloneVM_Task($targetfolder.MoRef, "PCClone2", $VMCloneSpec)

Where:

- you need to replace the {} at Reflection.Assembly with brackets

- you need to provide the servername, username and password parameters

- you need to change PCCore to the name of the VM you want to clone

I would like to have a bit more customization if possible. Smiley Happy

The SAN on which the core machine is ran out of space. I would like to clone it to one of the other SANs?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can specify the target data store via $VMCloneSpec.location.datastore.

You will have to provide a MORef to the target data store.

The line you would need to add:

$VMCloneSpec.Location.datastore = (Get-View (Get-Datastore -Name <name of target data store>).ID).MoRef


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dreampost
Contributor
Contributor
Jump to solution

Thanks a lot mate.

You helped me a lot. Smiley Happy

Reply
0 Kudos
SetClear
Enthusiast
Enthusiast
Jump to solution

So does this mean i can clone a live/running VM?

or does it still need to be powered down to clone.

Im looking for a means to clone live using a powershell script but not having any luck...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, you can use the CloneVM_Task method on a live guest (from ESX 3.5 u2 onwards).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
justin_emerson
Enthusiast
Enthusiast
Jump to solution

I have created a function to do this in a little more easy-to-use way with some comments and instructions. See attached.

Reply
0 Kudos