C # cannot connect to vmware

I have a project where I will have to create virtual machines with double glass. I usually work with powershell, but it seems to be unable to do this. I may have to use C #. I am a bit like this, but for some reason this code gives me the error "Unable to instantiate an 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 a link to the project. I must have forgotten to do something.

+5
source share
1 answer

According to https://communities.vmware.com/thread/478700 : "either stick to PowerCLI version 5.5, as mentioned, or change the code to use the VimClientImpl class instead of VimClient (which is now an interface)."

A complete simple example that 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(" admin@vsphere.local ", "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 link to "C: \ Program Files (x86) \ VMware \ Infrastructure \ vSphere PowerCLI \ VMware.Vim.dll". Update hostname, password; and the will!

+5
source

Source: https://habr.com/ru/post/1202351/


All Articles