Click or drag to resize

Sample

A code sample of using VMInfoFace

Requirement

Install SOWI VMInfoFace package see Install

MS Visual Studio Project References see Preparation Development VMInfoFace Use

Sample Application

Login to vSphere centre and read virtual machines

C#
// TODO set login data and option
string Server = "";
string UserName = "";
string Password = "";
bool WriteSummary = false;              //* performance *
string NetworkConfigurationFile = "";

VMInfoFace.VCenter.GetMachines(Server, UserName, Password, WriteSummary, NetworkConfigurationFile);

See also VCenter.GetMachines(String, String, String, Boolean, String)

Virtual machine data writes to console

C#
List<VMInfoFace.Data.Machine> Machines = VMInfoFace.VCenter.Machines;

foreach (VMInfoFace.Data.Machine Machine in Machines)
{
    Console.WriteLine(Machine.Name.ToString());
    Console.WriteLine("Datacenter:                  " + Machine.Datacenter.ToString());
    Console.WriteLine("Path:                        " + Machine.Path.ToString());
    Console.WriteLine("OS:                          " + Machine.OS.ToString());
    Console.WriteLine("CPU:                         " + Machine.CPU.ToString());
    Console.WriteLine("RAM in MB:                   " + Machine.RAM.ToString());
    Console.WriteLine("Dedicated graphics card:     " + Machine.GraphicsCard.ToString());
    Console.WriteLine("Screen width x height:       " + Machine.Screen.Width.ToString() + " x " + Machine.Screen.Height.ToString());
    Console.WriteLine("HD in MB:                    " + Machine.HD.ToString());
    Console.WriteLine("Network name:                " + Machine.NetworkName.ToString());
    Console.WriteLine("Network Adapters:            " + Machine.NetworkAdapter.ToString());
    Console.WriteLine("Network Bandwidth:           " + Machine.NetworkBandwidth.ToString());
    Console.WriteLine("Host Name:                   " + Machine.HostName.ToString());
    Console.WriteLine("IP:                          " + Machine.IP.ToString());
    Console.WriteLine("IPs:");
    foreach (var IP in Machine.IPs)
    {
        Console.WriteLine("                             " + IP.ToString());
    }
    Console.WriteLine("Disks:");
    foreach (var Disk in Machine.Disks)
    {
        Console.WriteLine("                             " + Disk.ToString());
    }
    Console.WriteLine("Storages in MB:");
    foreach (VMInfoFace.Data.Storage Storage in Machine.Storages)
    {
        Console.WriteLine("                             " + Storage.Name.ToString() + ": " + Storage.Capacity.ToString());
    }
    if (Machine.Details.Length > 0)
    {
        Console.WriteLine(new string('-', 50));
        Console.WriteLine("Summary");
        Console.WriteLine(Machine.Details.ToString());
    }
}

More information about virtual machine data and code examples see VMInfoFace.DataMachine

More information about virtual machine data value see InventoryWrite

Machine data item formatting handling

This example has function for string handling

Virtual machine data handling by StringBuilder and string handling functions (event list click build details information)

C#
/// <summary>
/// Compile of details data
/// </summary>
/// <param name="pPageListDetails">VM list and details</param>
/// <param name="pItem">VM data</param>
/// <seealso cref="SOWIWin.Helper.Pages.PageListDetails.Selected"/>
private void PageMachine_Selected(SOWIWin.Helper.Pages.PageListDetails pPageListDetails, SOWIData.Helper.Interface.IListDetails pItem)
{
    StringBuilder lText = new StringBuilder();
    VMInfoFace.Data.Machine lMachine = (VMInfoFace.Data.Machine)pItem;
    lText.AppendLine(lMachine.Name.ToString());
    lText.AppendLine(GetLabel("Datacenter:") + lMachine.Datacenter.ToString());
    lText.AppendLine(GetLabel("Path:") + lMachine.Path.ToString());
    lText.AppendLine(GetLabel("OS:") + lMachine.OS.ToString());
    lText.AppendLine(GetLabel("CPU:") + GetValue(lMachine.CPU));
    lText.AppendLine(GetLabel("RAM in MB:") + GetValue(lMachine.RAM));
    lText.AppendLine(GetLabel("Dedicated graphics card:") + lMachine.GraphicsCard.ToString());
    lText.AppendLine(GetLabel("Screen width x height:") + lMachine.Screen.Width.ToString() + " x " + lMachine.Screen.Height.ToString());
    lText.AppendLine(GetLabel("HD in MB:") + GetValue(lMachine.HD));
    lText.AppendLine(GetLabel("Network name:") + lMachine.NetworkName.ToString());
    lText.AppendLine(GetLabel("Network Adapters:") + GetValue(lMachine.NetworkAdapter));
    lText.AppendLine(GetLabel("Network Bandwidth:") + GetValue(lMachine.NetworkBandwidth));
    lText.AppendLine(GetLabel("Host Name:") + lMachine.HostName.ToString());
    lText.AppendLine(GetLabel("IP:") + lMachine.IP.ToString());
    lText.AppendLine(GetLabel("IPs:"));
    foreach (var lIP in lMachine.IPs)
    {
        lText.AppendLine(GetLabel(" ") + lIP.ToString());
    }
    lText.AppendLine("Disks:");
    foreach (var lDisk in lMachine.Disks)
    {
        lText.AppendLine(GetLabel(" ") + lDisk.ToString());
    }
    lText.AppendLine("Storages in MB:");
    foreach (VMInfoFace.Data.Storage lStorage in lMachine.Storages)
    {
        lText.AppendLine(GetLabel(" ") + lStorage.Name.ToString() + ": " + new string(' ', 35 - (lStorage.Name.Length + lStorage.Capacity.ToString().Length)) + lStorage.Capacity.ToString());
    }
    if (lMachine.Details.Length > 0)
    {
        lText.AppendLine(new string('-', 50));
        lText.AppendLine("Summary");
        lText.AppendLine(pItem.Details.ToString());
    }
    pItem.Details = lText.ToString();

}

Function for label text handling (method: GetLabel)

C#
/// <summary>
/// Builds a label for details information
/// </summary>
/// <param name="pLabel">Label text. Example: "Network name:"</param>
/// <returns>Label text with space. Example: "Network name:           "</returns>
/// <remarks>
/// If triggers an exception then gives back the exception message
/// </remarks>
private string GetLabel(string pLabel)
{
    int lWidth = 27;
    try
    {
        if (pLabel.Length < lWidth)
        {
            return pLabel + new string(' ', lWidth - pLabel.Length);
        }
        else
        {
            return pLabel;
        }
    }
    catch (Exception ex)
    {
        return "#Exception (GetLabel): " + ex.Message.ToString();
    }
}

Function for value handling (method: GetValue)

C#
/// <summary>
/// Formatted the value for details information
/// </summary>
/// <param name="pValue">Value to formatting (text or number)</param>
/// <returns>Value formatted (text to left alignment, number is to right alignment)</returns>
/// <remarks>
/// If triggers an exception then gives back the exception message
/// </remarks>
private string GetValue(object pValue)
{
    int lWidth = 20;
    try
    {
        if (pValue.IsNumber())
        {
            return new string(' ', lWidth - pValue.ToString().Length) + pValue.ToString();
        }
        return pValue.ToString();
    }
    catch (Exception ex)
    {
        return "#Exception (GetValue): " + ex.Message.ToString();
    }
}

Sample Application download link

VMInfoFaceSample.zip

See Also