我应该使用哪个 VMware API?

发布于 2024-10-27 22:19:04 字数 1431 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-11-03 22:19:04

我感受到你的痛苦。我正在对他们的 API 有多痛苦进行更长的咆哮,但我不会打扰你。以下是对我来说相当有效的方法(我直接连接到 ESX 机器,但我认为您应该能够在此基础上构建以到达您想要去的地方):(

编辑:格式已修复)

  1. 获取 vSphere PowerCLI 此处(以前称为 VI Toolkit (对于 Windows));它包括 VMware.Vim API 和接口所遵循的所需底层实现类(当然,从阅读文档来看后者并不明显)。将其安装在您的开发计算机上(这会将 .NET VMware.Vim 实现库放入全局程序集缓存中;稍后我们将提取我们关心的可移植性库)

  2. 创建一个 VS 项目,并放入一些 hello world代码。

    使用VMware.Vim;
    
    //一些命名空间,一些类,一些方法...
    
    VimClient c = new VimClient();
    ServiceContent sc = c.Connect("主机名OrIpHere");
    UserSession us = c.Login("用户名这里", "密码这里");
    
    IList vms = c.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, null, null);
    foreach(虚拟机中的VMware.Vim.EntityViewBase tmp)
    {
        VMware.Vim.VirtualMachine vm = (VMware.Vim.VirtualMachine)tmp;
        Console.WriteLine((bool)(vm.Guest.GuestState.Equals("正在运行") ? true : false));
        Console.WriteLine(new Uri(ENDPOINTURL_PREFIX + (vm.Guest.IpAddress != null ? vm.Guest.IpAddress : "0.0.0.0") + ENDPOINTURL_SUFFIX));
        Console.WriteLine((string)vm.Client.ServiceUrl);
        Console.WriteLine(vm.Guest.HostName != null ? (string)vm.Guest.HostName : "");
        Console.WriteLine("----------------");        
    

    }

  3. 如果这有效并打印出有关您的虚拟机的一些信息,那么到目前为止,一切都很好。如果您看到类似 System.IO.FileNotFoundException: Could not load file or assembly 'VimService40, Version=4.0.0.0, Culture=neutral, Public KeyToken=10980b081e887e9f' 或其依赖项之一。系统找不到指定的文件。那么你就知道你没有安装实际的实现文件:VMware.Vim.dll只是接口,实际的每协议实现位于VimService40.dll等文件中您应该通过步骤 1 获得该信息。

  4. 一旦您想要将此代码部署到某处,您必须随其发送实际的实现 dll(同样,VMware.vim.dll 是不够的)。您可以使用命令行(不是资源管理器,它不起作用)将它们从全局程序集缓存中复制出来。

    从 GAC 获取 VimService DLL:

    cd %windir%\Assembly\GAC_MSIL
    cp VimService20\2.0.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService20.XmlSerializers\2.0.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    cp VimService25\2.5.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService25.XmlSerializers\2.5.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    ...等等,对于您可能使用的所有 VimService 版本...
    
  5. 当您将代码部署到另一台计算机时,将这些 DLL 放在同一文件夹(或路径上),这样您就应该为构建和部署与 ESX 机器配合使用的代码奠定了良好的基础。< /p>

I feel your pain. I'm sitting on a longer rant about how painful their APIs are, but I'll spare you. Here's what worked reasonably well for me (I am connecting directly to ESX boxes, but I think you should be able to build on this to get where you want to go):

(Edit: Formatting fixed)

  1. Grab the vSphere PowerCLI here (previously called VI Toolkit (for Windows)); it includes the VMware.Vim API and the required underlying implementation classes that interface defers to (though naturally, the later is not obvious from reading the docs). Install it on your dev machine (this puts the .NET VMware.Vim implementation libraries in your Global Assembly Cache; we'll extract the libraries we care about for portability later)

  2. Create a VS project, and throw in some hello world code.

    using VMware.Vim;
    
    //some namespace, some class, some method...
    
    VimClient c = new VimClient();
    ServiceContent sc = c.Connect("hostnameOrIpHere");
    UserSession us = c.Login("usernameHere", "passwordHere");
    
    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(new Uri(ENDPOINTURL_PREFIX + (vm.Guest.IpAddress != null ? vm.Guest.IpAddress : "0.0.0.0") + ENDPOINTURL_SUFFIX));
        Console.WriteLine((string)vm.Client.ServiceUrl);
        Console.WriteLine(vm.Guest.HostName != null ? (string)vm.Guest.HostName : "");
        Console.WriteLine("----------------");        
    

    }

  3. If that works and prints out some info about your VMs then so far, so good. If you see something like System.IO.FileNotFoundException: Could not load file or assembly 'VimService40, Version=4.0.0.0, Culture=neutral, Public KeyToken=10980b081e887e9f' or one of its dependencies. The system cannot find the file specified. then you know you don't have the actual implementation files installed: VMware.Vim.dll is just the interface, and the actual per-protocol implementations are in files like VimService40.dll that you should have gotten with step 1.

  4. Once you want to deploy this code somewhere, you have to send the actual implementation dlls with it (again, VMware.vim.dll isn't sufficient). You can use the command line (not Explorer, it won't work) to copy them out of the Global Assembly Cache.

    Get VimService DLL from GAC:

    cd %windir%\assembly\GAC_MSIL
    cp VimService20\2.0.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService20.XmlSerializers\2.0.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    cp VimService25\2.5.0.0__10980b081e887e9f\VimService20.dll %HOMEDRIVE%\%HOMEPATH%\Desktop
    cp VimService25.XmlSerializers\2.5.0.0__10980b081e887e9f\VimService20.XmlSerializers.dll %HOMEDRIVE%\%HOMEPATH%
    ... etc, for all the VimService versions you might use ...
    
  5. When you deploy your code to another machine, put those DLLs in the same folder (or on the path) and you should have a decent basis for building and deploying code that works with ESX boxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文