VMware {code} Community
jlegan
Contributor
Contributor

Could I get someone to review this? - C# CloneVM issue

So I am using the VI Toolkit and I am determined to make it work. I know this is not the correct forum for this, but there is not a lot of non-powershell activity in the VI Toolkit community and I would appreciate if someone could review the code to see what might be happening here. The only clue I have right now is that in addition to the CloneVM_Task throwing an "The operation is not supported on the object", so is "CreateFolder" against my Datacenters VmFolder.

This is a complete dump of the method. Note that all other automation tasks (stopping, starting, enumerating, etc... ) are all working flawlessly, so I am pretty sure that my datacenter, vm and hostsystem objects are working correctly. I think it might be folder related but I am not familar enough to know.

PS: This is an ESXi server and the source VM is not a template in case that matters.

Thanks,

Jim

        // 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;
        }

Tags (2)
Reply
0 Kudos
2 Replies
jlegan
Contributor
Contributor

I think I just figured out my issue... We don't have VirtualCenter and as a result this is not an option.

Reply
0 Kudos
njain
Expert
Expert

Cloning is a feature of VirtualCenter and not ESX server. Thus, you would not be able to clone a VM by directly connecting to the ESX server. You can also refer to the product features for more details on Virtual Center

http://www.vmware.com/products/vi/vc/features.html

On the other hand, the source that you would like to clone can either be a Virtual Machine or a template. If the virtual machine is used as a template, the CloneVM_Task method corresponds to the deploy command. You can also refer to the API reference guide for more details on this method.

Reply
0 Kudos