Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# cannot connect to vmware

Tags:

c#

vmware

I have a project where i will have to build dual stacked virtual machines. I usually work with powershell but it does not appear to be able to do that. I may have to use C#. I am kinda rusty on this but for some reason this code give me an error "Cannot create an instance of the abstract class or interface 'VMware.Vim.VimClient'".

using System.Text;
using VMware.Vim;

namespace Vimfunctions
{  

    public class VimFunctions
    {
        protected VimClient ConnectServer(string viServer, string viUser, string viPassword)
        {
            **VimClient vClient = new VimClient();**
            ServiceContent vimServiceContent = new ServiceContent();
            UserSession vimSession = new UserSession();

            vClient.Connect("https://" + viServer.Trim() + "/sdk");
            vimSession = vClient.Login(viUser, viPassword);
            vimServiceContent = vClient.ServiceContent;

            return vClient;
        }

I added the reference to the project. I must have forgot to do something.

like image 368
user873577 Avatar asked Sep 11 '14 14:09

user873577


1 Answers

As per https://communities.vmware.com/thread/478700: "either stick with the PowerCLI 5.5 release as mentioned or to modify your code to use the VimClientImpl class instead of VimClient (which is now an interface)."

A complete simple example I used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VMware.Vim;

namespace vSphereCli
{
    class Program
    {
        static void Main(string[] args)
        {
            VMware.Vim.VimClientImpl c = new VimClientImpl();
            ServiceContent sc = c.Connect("https://HOSTNAME/sdk");
            UserSession us = c.Login("[email protected]", "password");
            IList<VMware.Vim.EntityViewBase> vms = c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null);
            foreach (VMware.Vim.EntityViewBase tmp in vms)
            {
                VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp;
                Console.WriteLine((bool)(vm.Guest.GuestState.Equals("running") ? true : false));
                Console.WriteLine(vm.Guest.HostName != null ? (string)vm.Guest.HostName : "");
                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}

Add a reference to "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\VMware.Vim.dll". Update the hostname, password; and volia!

like image 196
Dane W Avatar answered Sep 29 '22 23:09

Dane W