Automation

 View Only
  • 1.  vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 10:33 AM

    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



  • 2.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 10:34 AM

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

    }



  • 3.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 11:03 AM

    Is there an error message ?



  • 4.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 11:08 AM

    LOL, that would have been helpful :smileyhappy:

    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



  • 5.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 01:03 PM

    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.



  • 6.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 01:34 PM

    Hmmmm, is the same true of CreateFolder()?



  • 7.  RE: vSphere PowerCLI / C# / CloneVM_Task
    Best Answer

    Posted Aug 25, 2009 01:42 PM

    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.



  • 8.  RE: vSphere PowerCLI / C# / CloneVM_Task

    Posted Aug 25, 2009 02:21 PM

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