【问题标题】:c# API getting VM name from MORc# API 从 MOR 获取 VM 名称
【发布时间】:2018-03-30 22:57:36
【问题描述】:

我可以获得 VM 的 MOR 列表,但有没有办法将其转换为实际的 VM 名称? 我使用带有 Traversalspec 的 PropertySpec 来获取 VM 的 MOR,但是如何将其转换为实际名称。 例如,我有“VirtualMachine-15”。我找不到获取名称的方法。

我正在使用 vmware.vim.dll。

以下是获取 VM 的 MOR 的部分代码:

 ObjectContent[] objs = pCollector.RetrieveProperties(pFss);

        for (int i = 0; i < objs.Length; i++)
        {


            DynamicProperty[] dps = objs[i].PropSet;

            foreach (DynamicProperty d in dps)
            {

                if (d.Name == "childEntity")
                {
                    ManagedObjectReference[] vmMORs = (ManagedObjectReference[])d.Val;

                    foreach(ManagedObjectReference vmMOR in vmMORs )
                        {

                        listview1.Items.Add(vmMOR);

                        }
                }
            }
        }

谢谢

【问题讨论】:

    标签: c# vmware vsphere esxi


    【解决方案1】:

    不管怎样,经过一天的反复试验,我遇到了一个类似的问题:

    VimClientImpl client = new VimClientImpl();
    client.Login(vSphereURI, loginID, loginPW);
    List<EntityViewBase> vmlist = client.FindEntityViews(typeof(VirtualMachine), null, filter1, null);
    List<EntityViewBase> hostlist = client.FindEntityViews(typeof(HostSystem), null, null, null);
    
    foreach (HostSystem host in hostlist)
    {
       host.UpdateViewData();
       Console.WriteLine(@"# Host: " + host.Name + @": ");
    
       for (int i = 0; i < host.Vm.Length; i++)
       {
          VirtualMachine vm = (VirtualMachine)client.GetView(host.Vm[i], null);
          Console.WriteLine(vm.Name);
       }
    }
    
    foreach (VirtualMachine vm in vmlist)
    {
       vm.UpdateViewData();
       HostSystem host = (HostSystem)client.GetView(vm.Runtime.Host, null);
       Console.WriteLine("name: " + vm.Name + " >>lives on>> " + host.Name);
    }
    

    基本上,我必须将 MOR 传递给 client.GetView(MOR, null),然后我发现自己为关联的主机或 vm 提供了一个新的托管对象,具体取决于视角。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2021-05-07
      • 1970-01-01
      相关资源
      最近更新 更多