VMware Cloud Community
jlegan
Contributor
Contributor
Jump to solution

vSphere PowerCLI / C# / CloneVM_Task

I have written a fairly extensive web service for my own needs that automates operations in all of the major virtualization providers (Virtual Server 2005, Hyper-V, ESX, XEN, etc...). However, one element of ESX management has me stumped and I think i have it narrowed down to my folder object (which moref I have), but I am not sure where to go with it. If someone has run into this before, I would greatly appreciate some insight. Sample code is below.

Thanks,

Jim

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm afraid so.

You can test this by connecting the VIC directly to an ESX server.

You will notice that a lot of the features you have when you are connected to the VC are not there.


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

View solution in original post

Reply
0 Kudos
7 Replies
jlegan
Contributor
Contributor
Jump to solution

// Clones a virtual machine and creates new machines based on the comma sep. string passed in.

public bool CloneVirtualSystem()

{

// Init everything we will need.

bool fRet = false;

bool fSuccess = false;

VirtualMachine oVirtualMachine = null;

ManagedObjectReference morTask = new ManagedObjectReference();

VirtualMachineCloneSpec oVirtualMachineCloneSpec = new VirtualMachineCloneSpec();

VirtualMachineRelocateSpec oVirtualMachineRelocateSpec = new VirtualMachineRelocateSpec();

Datastore oDatastore = null;

Task oTask = null;

Folder oFolder = null;

try

{

// Try and connect to the host

fSuccess = ConnectToEsxServer(m_sUsername, m_sPassword, m_sEsxServer);

// If we successfully connected, get the virtual machine object

if (fSuccess)

{

oVirtualMachine = GetVirtualMachineObject();

}

// If we have a valid virtual machine object, proceed

if (oVirtualMachine != null)

{

// Grab the datastore for the current VM (the new VM will be created in the same location

oDatastore = (Datastore)m_oVimClient.GetView(oVirtualMachine.Datastore[0], null);

// Setup the relocate and clone spec.

oVirtualMachineRelocateSpec.Host = m_oHostSystem.MoRef;

oVirtualMachineRelocateSpec.Pool = oVirtualMachine.ResourcePool;

oVirtualMachineRelocateSpec.Datastore = oDatastore.MoRef;

oVirtualMachineRelocateSpec.Transform = VirtualMachineRelocateTransformation.sparse;

oVirtualMachineCloneSpec.Template = false;

oVirtualMachineCloneSpec.PowerOn = false;

oVirtualMachineCloneSpec.Location = oVirtualMachineRelocateSpec;

// Grab the vm folder from the datacenter object

oFolder = (Folder)m_oVimClient.GetView(m_oDatacenter.VmFolder, null);

// Clone the machine

morTask = oVirtualMachine.CloneVM_Task(oFolder.MoRef, "TestClone", oVirtualMachineCloneSpec);

// Grab the task

oTask = (Task)m_oVimClient.GetView(morTask, null);

// Monitor it for completion

fRet = EvalulateVmTask(ref oTask, ref morTask);

}

}

catch (VimException exp)

{

m_oLogging.Error(m_sVirtualMachine + ": CloneVirtualSystem() failed with error " + exp.Message);

Console.Write(m_sVirtualMachine + ": CloneVirtualSystem() failed with error " + exp.Message);

return fRet;

}

catch (Exception exp)

{

m_oLogging.Error(m_sVirtualMachine + ": CloneVirtualSystem() failed with error " + exp.Message);

Console.Write(m_sVirtualMachine + ": CloneVirtualSystem() failed with error " + exp.Message);

return fRet;

}

// Return the results

return fRet;

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is there an error message ?


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

Reply
0 Kudos
jlegan
Contributor
Contributor
Jump to solution

LOL, that would have been helpful Smiley Happy

The exception that is tossed is: The operation is not supported on the object.

To test my theory that it is related to the folder object (which is a valid object, I can see 18 VM's within it) I tried to call the CreateFolder() method of the folder object and got the same thing. I don't think that the Datacenter.VmFolder is the correct folder to be using but I could be off base. It looks like another user ran into the exact same issue here: http://communities.vmware.com/message/1075808#1075808

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The CloneVM_Task is, afaik, not supported when you connect to an ESX server.

You have to connect to a VC to be able to use this method.


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

Reply
0 Kudos
jlegan
Contributor
Contributor
Jump to solution

Hmmmm, is the same true of CreateFolder()?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid so.

You can test this by connecting the VIC directly to an ESX server.

You will notice that a lot of the features you have when you are connected to the VC are not there.


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

Reply
0 Kudos
jlegan
Contributor
Contributor
Jump to solution

Thanks for the info, I can stop banging my head against a wall now.

Reply
0 Kudos