Automation

 View Only
  • 1.  c# CloneVM_Task

    Posted Aug 08, 2008 02:38 PM

    I read the great post about cloning a VM with PowerShell here: http://communities.vmware.com/message/999650#999650.

    I am creating a simple interface to clone virtual machines and I have run into an issue with the first parameter of CloneVM_Task.

    It expects a ManagedObjectReference to a folder. What is not clear from the API documentation is how to obtain that MOR and whether or not I need to create the folder ahead of time or if it will be created for me automatically (based on the name of the new machine).

    When I try and instantiate a new Folder object (which exposes create folder which returns a folder MOR), it expects folder MOR which I do not yet have. Also, the VimClient does not expose a Get-Folder equiv that the PowerShell example uses.

    Does anyone have a code example in C# for cloning a VM?

    Thanks,

    Jim



  • 2.  RE: c# CloneVM_Task

    Posted Aug 08, 2008 07:01 PM

    This has got me stumped. Does anyone have a code snippet for simple getting a ManagedObjectReference for a folder object? That is all I need to get this to work and I have been playing with it for the last few hours.

    Thanks,

    Jim



  • 3.  RE: c# CloneVM_Task

    Posted Aug 10, 2008 01:17 PM

    I have been playing around with this all weekend and I still have not gotten past it. Has anyone out there besides me tried to utilize CloneVM_Task in c# with the VI Toolkit?

    I know my issue is in getting the ManagedObjectReference to the folder. What I am not clear on is if I need to create that folder or if it is simply looking for the root (/) folder and it will subsequently create the folder for that VM for me. Either way, I cannot get the MOR for that folder (root). This may be related to that fact that I have two datastores on this server. However, I created a HostSystem object as well and I can see those datastores but I still cannot figure out how to get a proper folder object.



  • 4.  RE: c# CloneVM_Task

    Broadcom Employee
    Posted Aug 13, 2008 05:04 PM

    Hi,

    The easiest way to get a folder is using FindEntityView method of the VimClient object. The code below uses filter by name to get the default (root) VM folder named "vm"

             VimClient client = new VimClient();
     
             ...
     
             NameValueCollection filter = new NameValueCollection();
             filter.Add("Name", "vm");
     
             Folder vmFolder = (Folder) client.FindEntityView(typeof(Folder), null, filter, null)
     
    

    Using vmFolder object and already created VirtualMachineCloneSpec you can now call CloneVM

             sourceVM.CloneVM(vmFolder.MoRef, "newVM", cloneSpec);
    

    Regards,

    Yasen



  • 5.  RE: c# CloneVM_Task

    Posted Jun 09, 2011 08:26 PM

    I am attempting to move a bunch of VM's into a folder, all in one shot. In the snippet below, the string id for the Folder MOR is hardcoded and running this worked once but if I run again, it says the MOR has not been completely created or deleted. I tried the default constructor to no avail, random numbers.. same error as before.

    How can I predict these numbers? Drag and drop on vSphere Client is much easier. How can I automate this??

    public static void CreateFolder(VimClient client, string foldername, params string[] vmNames)
            {
                // ------- CreateFolder -------

                Folder folder = new Folder(client, new ManagedObjectReference("Folder-group-v3"));
                folder.CreateFolder(foldername);

                // ------- MoveIntoFolder_Task -------
               
                foreach (string t in vmNames)
                {
                    var filter = new NameValueCollection { { "name", "^" + t + "$" } };
                    var vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, filter, null);
                    if (vm == null)
                    {
                        Console.WriteLine(t + " is not a valid virtual Machine name.");
                    }
                    else
                    {
                        ManagedObjectReference[] list = new ManagedObjectReference[1];
                        list[0] = new ManagedObjectReference {Type = "VirtualMachine", Value = vm.MoRef.Value};
                        Folder _this = new Folder(client, new ManagedObjectReference("Folder-group-v471"));        //************* harcoded value**********
                        ManagedObjectReference m = _this.MoveIntoFolder_Task(list);
                        AwaitTaskCompletion(client, m);
                    }
                }

            }