Get Managed Object
We can easily gather all Managed Object information using vSphere SDK. We have seen in the earlier post about How to connect using SDK. Now, we will see how you can gather the Managed Object information using SDK.
Once, we connect to vCenter, we will have an object called VMware.Vim.VimClientImpl and after connection, this object will help us to gather any Managed Object information.
VimClientImpl object has the method FindEntityViews that can be used to get any Managed Object from the vCenter. This method requires the following parameters
- View Type
"ClusterComputeResource"
"ComputeResource"
"Datacenter"
"Datastore"
"DistributedVirtualSwitch"
"Folder"
"HostSystem"
"Network"
"ResourcePool"
"VirtualApp"
"VirtualMachine"
- Begin Entity – This is Managed object reference input, that specifies the starting point for search in the inventory. This parameter helps you narrow the scope but it can be null.
- Filter – This is NameValueCollection input that uses hash of one or more name-value pairs, we can filter the search. The name represents the property value to test and the value represents a pattern that the property must match. If more than one pair is specified, all the patterns must match. It can be null but in that case all objects will be returned based on the specified View Type.
- Properties – This is string array input that contains View Type object property names. This can be null but if we specify the list of required properties then performance increases as View Type contains thousands of properties hence it is always recommended to specify properties that are required. You can refer VMware official documentation to understand the Managed Object properties. e.g. VirtualMachine Managed Object
Method FindEntityViews return List of VMware.Vim.EntityViewBase object that needs iteration of the list. In the iteration, we will have to cast the respective View Type that is used in FindEntityViews method.
In the following example, we are gathering VirtualMachine information.
After casting, we get the vm object that can be used to get respective VirtualMachine properties or perform any action on the VirtualMachine object.
VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp;
vm object can be used to get properties such as Name, PowerState, vCPU, Memory many more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using VMware.Vim; namespace vSphere { class Program { static void Main(string[] args) { VMware.Vim.VimClientImpl client = new VimClientImpl(); ServiceContent sc = client.Connect("https://vCenterFqdn/sdk"); UserSession us = client.Login("user@domain", "password"); //We are now connected to vCenter and we can now do any task on the connected vCenter IList<VMware.Vim.EntityViewBase> vms = client.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null); foreach (VMware.Vim.EntityViewBase tmp in vms) { VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp; } } } } |