VMware {code} Community
zedragoune
Contributor
Contributor

How to find datastore of a VM from VM Name - VMWare.Vim.dll - C#

Hi,

Is there anyone that can tell me how to find vm details from the VM Name in C# ?

I tried using VMWare.Vim.dll but could not link existing datastore properties to an existing VM.

For VM, i could link the host of the VM with the following code but no more :

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

VimClient vimClient = new VimClientImpl();

ServiceContent vimServiceContent = new ServiceContent();

string sViServer = "<VcenterIpAddress>";

string sUsername = "<VcenterAdminLogin>";

string sPassword = "<VcenterAdminPasswd>";

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 Datastore, i have the following code but no link between the two parts of code :

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

}

}

If no solution i'll use a powercli script, but i would like to have all in c#.

Could you help me plz ?

Fred

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };
VimClient vimClient = new VimClientImpl();
ServiceContent vimServiceContent = new ServiceContent();
string sViServer = "192.168.15.12";
string sUsername = "administrator@vsphere.local";
string sPassword = "Fr4eder2.";
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);
}
 
}
0 Kudos
0 Replies