I'm fairly new to the vSphere API (I've done a lot more with VIX) and I'm trying to do something relatively simple which is to connect to my ESX server and then obtain a list of the VM names hosted there. From there I would eventually create code to ascertain the power status of the VMs (on, off, etc.) and do useful tasks but first things first. My C# code and output is below. As far as I can tell, I'm connecting fine to ESX and I'm not getting any errors of any kind but the data structure being returned just doesn't have any VM names. Any ideas would be appreciated. :-)
Program Output:
Starting Program.
Instantiated VIM Service.
Instantiated Service Content
About to connect to ESX Server.
Successfully connected to ESX server.
Before retrieve properties call.
After retrieve properties call but before for loop.
ocArray is not null
How big is the OC Array? 0
Press any key to exit
Source Code:
using System;
using System.Globalization;
using Vim25Api;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web.Services.Protocols;
namespace Connect {
public class Connect
{
private static ManagedObjectReference SIMO_REF = new ManagedObjectReference();
private static VimService VIM_SERVICE;
private static ServiceContent SERVICE_CONTENT;
private static String STR_SERVICE_INSTANCE = "ServiceInstance";
private static void connect(String url, String uname, String pword)
{
VIM_SERVICE.Login(SERVICE_CONTENT.sessionManager, uname, pword, null);
}
private static void disconnect()
{
VIM_SERVICE.Logout(SERVICE_CONTENT.sessionManager);
}
public static bool TrustAllCertificateCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
public static void Main(string[] args) {
try {
Console.WriteLine("Starting Program.");
SIMO_REF.type = STR_SERVICE_INSTANCE;
SIMO_REF.Value = STR_SERVICE_INSTANCE;
System.Net.ServicePointManager.ServerCertificateValidationCallback = Connect.TrustAllCertificateCallback;
VIM_SERVICE = new VimService();
try
{
VIM_SERVICE.Url = "https://10.1.1.20/sdk";
VIM_SERVICE.CookieContainer = new System.Net.CookieContainer();
}
catch (UriFormatException mue)
{
Console.WriteLine(mue.StackTrace);
}
catch (Exception se)
{
Console.WriteLine(se.StackTrace);
}
Console.WriteLine("Instantiated VIM Service.");
if (SERVICE_CONTENT == null)
{
try
{
SERVICE_CONTENT = VIM_SERVICE.RetrieveServiceContent(SIMO_REF);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
Console.WriteLine("Instantiated Service Content");
Console.WriteLine("About to connect to ESX Server.");
connect("https://10.1.1.20", "*******", "**********");
Console.WriteLine("Successfully connected to ESX server.");
PropertyFilterSpec spec = new PropertyFilterSpec();
PropertySpec[] propSpecArray = new PropertySpec[] { new PropertySpec() };
propSpecArray[0].type = "VirtualMachine";
spec.propSet = propSpecArray;
spec.objectSet = new ObjectSpec[] { new ObjectSpec() };
spec.objectSet[0].obj = SIMO_REF;
Console.WriteLine("Before retrieve properties call.");
ObjectContent[] ocArray = VIM_SERVICE.RetrieveProperties(SERVICE_CONTENT.propertyCollector, new PropertyFilterSpec[] {spec});
Console.WriteLine("After retrieve properties call but before for loop.");
if (ocArray == null)
{
Console.WriteLine("ocArray is null");
}
else
{
Console.WriteLine("ocArray is not null");
}
Console.WriteLine("How big is the OC Array? " + ocArray.Length);
if (ocArray != null)
{
for (int i = 0; i < ocArray.Length; ++i)
{
Console.WriteLine(" in ocArray loop on iteration " + i);
ObjectContent oc = ocArray[i];
DynamicProperty[] dps = oc.propSet;
if (dps != null)
{
Console.WriteLine("DPS is not null and is " + dps.Length + " elements long");
for (int j = 0; j < dps.Length; ++j)
{
Console.WriteLine("in DPS for loop iteration " + j + " and dps to string is " + dps[j].ToString());
DynamicProperty dp = dps[j];
Console.WriteLine("Name of property coming back from ESX " + dp.name);
//for (int p = 0; p < ret.Length; ++p)
//{
//if (properties[p].Equals(dp.name))
//{
// ret[p] = dp.val;
//}
//}
}
}
}
}
Console.WriteLine("Press any key to exit");
Console.Read();
}
catch (Exception e)
{
Console.WriteLine("Hit an error");
Console.WriteLine(e.ToString());
Console.WriteLine(e.StackTrace);
Console.WriteLine("Press any key to exit");
Console.Read();
}
finally
{
try
{
disconnect();
}
catch (Exception e)
{
Console.WriteLine("Error during finally...");
Console.WriteLine(e.ToString());
Console.WriteLine(e.StackTrace);
Console.WriteLine("Press any key to exit");
Console.Read();
}
}
}//end method
}//end class
}//end namespace