I have this code,it can show me the virtual machine's name of a vcenter:
using VMware.Vim;
VimClient c = new VimClient();
ServiceContent sc = c.Connect("hostnameOrIpHere");
UserSession us = c.Login("usernameHere", "passwordHere");
IList<VMware.Vim.EntityViewBase> vms =
c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null);
foreach (VMware.Vim.EntityViewBase tmp in vms)
{
var vm=(VMware.Vim.VirtualMachine)tmp;
Console.WriteLine(vm.name);
}
I use vmware powercli 5.5.If I use 6.0,fail with this:
VimClient c = new VimClient();
got error can't create instance of Interface VimClient
I want to got name,resource pool name,vlan id of a vm.I can use vm.name or vm.Config.name to got the name of a vm,but I tried many times,can't got resource pool name and vlan id of a vm
there is a property: vm.ResourcePool,got I got value like "ResourcePool-resgroup -409",I don't what is it.
and I don't know how to got the vlan id
Can you help me?Thanks!I can use other version of vmware powercli
You normally dont create an instance of VimClient, you create the instance of VimClientImpl().
VimClient client = new VimClientImpl();
I have used the following in the past to get VirtualMachine objects via following
List<VirtualMachine> vms =
c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null).Cast<VirtualMachine>().ToList();
Once you have the list of all the VMs.. you can look up different views for different object types, incl, PoolName, VLAN ID etc...
foreach (VirtualMachine vm in vms) {
Datastore ds = (Datastore)client.GetView(vm.Datastore.First(), null);
HostSystem host = (HostSystem)client.GetView(vm.Runtime.Host, null);
ClusterComputeResource cluster = (ClusterComputeResource)client.GetView(host.Parent, null);
ResourcePool rpool = (ResourcePool)client.GetView(vm.ResourcePool, null);
}
Getting the VLAN ID would be a bit tricky since you could have either kinds of network interfaces on your VM. I would recommend checking what kind of network interface you have to get the view for it and then look up the VLAN ID of that. Since VLAN is only available in the network attached, you'll first get the network (I have DistributedVirtualPortgroup) then get the properties of that to get the VLAN ID.
vm.Network -> list of network interfaces
Hope it helps someone else looking for this as well.