C#Vim.VMWare-从清单中添加

时间:2018-12-02 13:20:28

标签: c# vim vmware

使用C#上的VMware.Vim库,我尝试将VM重新添加到清单中(在VMWare中,通常通过右键单击.vmx文件,然后选择“添加到清单”来完成)。

我已经找到了如何从清单(“ UnregisterVM()”)中删除VM,如何从电源外壳向清单中添加VM(“ New-VM -VMFilePath $ VMXFile -VMHost $ ESXHost -Location $ VMFolder”),但是我找不到使用C#和VMware.Vim的方法。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

我设法找到了它。

在VMware.Vim中,“文件夹”类下有“ RegisterVM_Task”功能。

VMware文档:https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.Folder.html#registerVm

我的植入物(我确信它不是完美的,如果需要的话,我希望能得到纠正):

public void AddVMToInventory(string vmxFilePath, string vmName, string hostName, string resourcePoolName, string folderName)
    {
        try
        {
            // Filter host per host name/IP
            var hostFilter = new NameValueCollection { { "name", hostName } };
            HostSystem host = ((HostSystem)m_client.FindEntityView(typeof(HostSystem), null, hostFilter, null));
            var hostMoRef = ((HostSystem)m_client.FindEntityView(typeof(HostSystem), null, hostFilter, null)).MoRef;

            // Filter resource pool per resource pool name and host
            var poolFilter = new NameValueCollection { { "name", resourcePoolName }, { "parent", host.Parent.Value } };
            var poolMoRef = ((ResourcePool)m_client.FindEntityView(typeof(ResourcePool), null, poolFilter, null)).MoRef;

            // Filter folder per EXACT name
            var folderFilter = new NameValueCollection { { "name", "^" + folderName + "$" } };
            var folder = (Folder)m_client.FindEntityView(typeof(Folder), null, folderFilter, null);

            folder.RegisterVM_Task(vmxFilePath, vmName, false, poolMoRef, hostMoRef);

        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("Adding VM {0} to host {1} failed due to {2}", vmName, hostName, ex));
        }