VMware {code} Community
itnifl123
Contributor
Contributor
Jump to solution

How to retrieve a HostSystem via vmware.vim?

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?

Reply
0 Kudos
1 Solution

Accepted Solutions
itnifl123
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
1 Reply
itnifl123
Contributor
Contributor
Jump to solution

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;

      }

Reply
0 Kudos