Hello.
Im trying to locate a folder on my vCenter, and then list all the VM's thats inside that folder.
I took some code from the SimpleClient, however the recurse are really hard to understand for me.
I managed to modify the code, to find the folder "MKN".
Now all i need to do is to loop through every object/vm inside the folder. (Theres only gonna be VM's inside the folder.)
And this is the part where im stuck.
How can i define the path "MKN" is on, and then get all its content and loop through?
my code:
public void GetPowerStatus(string vName)
{
#region defines
// Create a Filter Spec to Retrieve Contents for...
TraversalSpec rpToVm = new TraversalSpec();
rpToVm.name = "rpToVm";
rpToVm.type = "ResourcePool";
rpToVm.path = "vm";
rpToVm.skip = false;
// Recurse through all ResourcePools
TraversalSpec rpToRp = new TraversalSpec();
rpToRp.name = "rpToRp";
rpToRp.type = "ResourcePool";
rpToRp.path = "resourcePool";
rpToRp.skip = false;
rpToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };
rpToRp.selectSet[0].name = "rpToRp";
rpToRp.selectSet[1].name = "rpToVm";
// Traversal through ResourcePool branch
TraversalSpec crToRp = new TraversalSpec();
crToRp.name = "crToRp";
crToRp.type = "ComputeResource";
crToRp.path = "resourcePool";
crToRp.skip = false;
crToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };
crToRp.selectSet[0].name = "rpToRp";
crToRp.selectSet[1].name = "rpToVm";
// Traversal through host branch
TraversalSpec crToH = new TraversalSpec();
crToH.name = "crToH";
crToH.type = "ComputeResource";
crToH.path = "host";
crToH.skip = false;
// Traversal through hostFolder branch
TraversalSpec dcToHf = new TraversalSpec();
dcToHf.name = "dcToHf";
dcToHf.type = "Datacenter";
dcToHf.path = "hostFolder";
dcToHf.skip = false;
dcToHf.selectSet = new SelectionSpec[] { new SelectionSpec() };
dcToHf.selectSet[0].name = "visitFolders";
// Traversal through vmFolder branch
TraversalSpec dcToVmf = new TraversalSpec();
dcToVmf.name = "dcToVmf";
dcToVmf.type = "Datacenter";
dcToVmf.path = "vmFolder";
dcToVmf.skip = false;
dcToVmf.selectSet = new SelectionSpec[] { new SelectionSpec() };
dcToVmf.selectSet[0].name = "visitFolders";
// Recurse through all Hosts
TraversalSpec HToVm = new TraversalSpec();
HToVm.name = "HToVm";
HToVm.type = "HostSystem";
HToVm.path = "vm";
HToVm.skip = false;
HToVm.selectSet = new SelectionSpec[] { new SelectionSpec() };
HToVm.selectSet[0].name = "visitFolders";
// Recurse thriugh the folders
TraversalSpec visitFolders = new TraversalSpec();
visitFolders.name = "visitFolders";
visitFolders.type = "Folder";
visitFolders.path = "childEntity";
visitFolders.skip = false;
visitFolders.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec() };
visitFolders.selectSet[0].name = "visitFolders";
visitFolders.selectSet[1].name = "dcToHf";
visitFolders.selectSet[2].name = "dcToVmf";
visitFolders.selectSet[3].name = "crToH";
visitFolders.selectSet[4].name = "crToRp";
visitFolders.selectSet[5].name = "HToVm";
visitFolders.selectSet[6].name = "rpToVm";
SelectionSpec[] selectionSpecs = new SelectionSpec[] { visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm };
PropertySpec[] propspecary = new PropertySpec[] { new PropertySpec() };
propspecary[0].all = false;
propspecary[0].allSpecified = true;
propspecary[0].pathSet = new string[] { "name" };
propspecary[0].type = "ManagedEntity";
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.propSet = propspecary;
spec.objectSet = new ObjectSpec[] { new ObjectSpec() };
spec.objectSet[0].obj = rootFolder;
spec.objectSet[0].skip = false;
spec.objectSet[0].selectSet = selectionSpecs;
// Recursively get all ManagedEntity ManagedObjectReferences
// and the "name" property for all ManagedEntities retrieved
ObjectContent[] ocary =
service.RetrieveProperties(
propCol, new PropertyFilterSpec[] { spec }
);
#endregion defines
ObjectContent oc = null;
ManagedObjectReference mor = null;
DynamicProperty[] pcary = null;
DynamicProperty pc = null;
for (int oci = 0; oci < ocary.Length; oci++)
{
oc = ocary[oci];
pcary = oc.propSet;
mor = oc.obj;
/*ListBox1.Items.Add("Object Type : " + mor.type);
ListBox1.Items.Add("Reference Value : " + mor.Value);*/
foreach (DynamicProperty value in pcary)
{
//ListBox1.Items.Add(value.val.ToString());
if (value.val.ToString() == "MKN")
{
ListBox1.Items.Add(value.name.ToString());
ListBox1.Items.Add(value.val.ToString());
}
}
/*if (pcary != null)
{
for (int pci = 0; pci < pcary.Length; pci++)
{
pc = pcary[pci];
if (pc != null)
{
if (!pc.val.GetType().IsArray)
{
ListBox1.Items.Add(pc.val.ToString());
}
}
}
}*/
}
}