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