0

I'm using the Vestris.VMWareLib API to remotely control my VMs on an ESX 5.0 server. I use the VMWareVirtualMachine.Open method to power on my virtual images. My code is written in C#. The problem is that you need to know the path to the datastore before you can power on the image - I'd like to be able to power it on knowing the VM name only. Is there a way to do this? I've included my current code below. Thanks, John

using Vestris.VMWareLib;

//Works if VM name is in the path but what if it isn't?
List<VMWareVirtualMachine> vitualMachines = esxServer.RegisteredVirtualMachines.ToList();
VMWareVirtualMachine virtualMachine = vitualMachines.Where(vm => vm.PathName.Contains(vmName)).First();
VMWareVirtualMachine virtualMachine = esxServer.Open(vmName);

There's a method called VMWareVirtualMachine.GetProperty() which can be used to obtain the VM name but I don't know how to use it. Any suggestions or ideas how I can do this?

Thanks, John

4

1 回答 1

1

有一个对VMWareTasks的提交,它将属性“Name”添加到 VMWareVirtualMachine 类,它取自 vmx 文件中的“displayName”属性。该属性不在 VMWareTasks 1.7 中,因此到目前为止,您需要提取源代码并自己构建它。

使用它来迭代已注册的客人,检查此变量,然后打开相应的电源。

using Vestris.VMWareLib;

private void powerOnVm(string vmName)
{
    using (VMWareVirtualHost esxServer = new VMWareVirtualHost())
    {
        esxServer.ConnectToVMWareVIServer("yourHost", "yourUser", "yourPassword");
        using (VMWareVirtualMachine virtualMachine = esxServer.RegisteredVirtualMachines.FirstOrDefault(vm => vm.Name == vmName))
        {
            if (virtualMachine != null && !virtualMachine.IsRunning)
                virtualMachine.PowerOn();
        }
    }
}

我刚刚测试了上面的,它工作正常。

于 2014-09-01T23:32:24.707 回答