0 Replies Latest reply on Feb 21, 2019 12:00 PM by zedragoune

    Find associated datastore from VM C# sdk

    zedragoune Lurker

      Hi,

       

      Is anyone able to tell me how to find all Vm details from the VM name in c# using VMWare.Vim.dll ?

      I found how to find host but not datastore for example.

      I searched in all properties of VM and datastore but could not find how to see the relationship between them.

       

      My code for VM and how to link it to vmware host :

       

      System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };

      VimClient vimClient = new VimClientImpl();

      ServiceContent vimServiceContent = new ServiceContent();

      string sViServer = "<ViServerIPAddress>";

      string sUsername = "<AdminVsphere>";

      string sPassword = "<Password>";

      vimClient.Connect("https://" + sViServer + "/sdk");

      vimClient.Login(sUsername, sPassword);

       

       

      vimServiceContent = vimClient.ServiceContent;

      NameValueCollection filter = new NameValueCollection();

      IList<VMware.Vim.EntityViewBase> vmList = vimClient.FindEntityViews(typeof(VirtualMachine), null, filter, null);

      foreach (VirtualMachine vm in vmList)

      {

      Console.WriteLine(vm.Name);

      VMware.Vim.CustomFieldValue[]  VmValues = vm.Value;

      foreach (VMware.Vim.CustomFieldValue VmValue in VmValues)

      {

      Console.WriteLine(VmValue.Key.ToString());

      }

      HostSystem host = (HostSystem)vimClient.GetView(vm.Runtime.Host, null);

      Console.WriteLine(host.Name);

      ManagedObjectReference[] DsList = vm.Datastore;

      foreach (ManagedObjectReference Ds in DsList)

      {

      Console.WriteLine(Ds.Type);

      Console.WriteLine(Ds.Value);

      }

      }

       

      For Datastores, i have this :

       

      IList<VMware.Vim.EntityViewBase> DatastoreList = vimClient.FindEntityViews(typeof(Datastore), null, filter, null);

      foreach (Datastore Datastore in DatastoreList)

      {

      Console.WriteLine(Datastore.Name);

      Console.WriteLine(Datastore.Value);

      ManagedObjectReference[] Vms = Datastore.Vm;

      foreach (ManagedObjectReference Vm in Vms)

      {

      Console.WriteLine(Vm.Type);

      Console.WriteLine(Vm.Value);

      }

      DatastoreHostMount[] Hosts = Datastore.Host;

      foreach (DatastoreHostMount host in Hosts)

      {

      Console.WriteLine(host.Key);

      }

      }

       

      But no value permits me to link an existing datastore to an existing VM.

       

      Could you help me plz ?

      Fred