itnifl123
Contributor
Contributor

How to retrieve a HostSystem via vmware.vim?

Jump to solution

I am trying to use the C# vmware sdk to retrieve a host represented as a HostSystem object. I have no datacenter and no cluster, only the single host I am connected to. How do I retrieve a HostSystem  representing that host?

0 Kudos
1 Solution

Accepted Solutions
itnifl123
Contributor
Contributor

After plenty of trial and error this works (C#):

      public List<HostSystem> GetHostSystems() {

         List<HostSystem> hostSystems = new List<HostSystem>();

         try {

            Datacenter dataCenter = (Datacenter)vSphereClient.FindEntityView(typeof(Datacenter), null, null, null);

            Folder folder = (Folder)vSphereClient.GetView(dataCenter.HostFolder, null);

            foreach(ManagedObjectReference mObjR in folder.ChildEntity.Where(x => x.Type == "ComputeResource")) {

               ComputeResource computeResource = (ComputeResource)vSphereClient.GetView(mObjR, null);

               foreach (ManagedObjectReference hostRef in computeResource.Host) {

                  hostSystems.Add((HostSystem)vSphereClient.GetView(hostRef, null));

               }

            }

         }

         catch (Exception e) {

            throw (e);

         }

         return hostSystems;

      }

View solution in original post

0 Kudos
1 Reply
itnifl123
Contributor
Contributor

After plenty of trial and error this works (C#):

      public List<HostSystem> GetHostSystems() {

         List<HostSystem> hostSystems = new List<HostSystem>();

         try {

            Datacenter dataCenter = (Datacenter)vSphereClient.FindEntityView(typeof(Datacenter), null, null, null);

            Folder folder = (Folder)vSphereClient.GetView(dataCenter.HostFolder, null);

            foreach(ManagedObjectReference mObjR in folder.ChildEntity.Where(x => x.Type == "ComputeResource")) {

               ComputeResource computeResource = (ComputeResource)vSphereClient.GetView(mObjR, null);

               foreach (ManagedObjectReference hostRef in computeResource.Host) {

                  hostSystems.Add((HostSystem)vSphereClient.GetView(hostRef, null));

               }

            }

         }

         catch (Exception e) {

            throw (e);

         }

         return hostSystems;

      }

View solution in original post

0 Kudos