在c#

时间:2019-01-01 09:43:14

标签: c# api vmware vsphere nic

这是我的代码,用于在VMware中禁用VM的网络接口卡。它运行完美,并且所有凭证都是正确的。它的编码与sdk文档中提到的相同。但是运行后,当我在VMWare上看到VM时,未禁用NIC。任何人都可以帮助我解决此问题。谢谢。

基本上,我们向客户DRaaS推广。因此,对于灾难恢复测试,我们尝试在同一VMWare上创建现有VM的新VM克隆。但是,当我们克隆虚拟机时,其NIC与现有虚拟机冲突。谢谢。

VMTypes.PlacementSpec vmPlacementSpec = new VMTypes.PlacementSpec();

VMTypes.FilterSpec VMFilterSpec = new VMTypes.FilterSpec();

HashSet<string> datacenters = new HashSet<string>
{
    GetDatacenter(serviceManager, vmModel.DataCenterName)
};

VMFilterSpec.SetNames(new HashSet<String> { vmModel.vmName });
VMFilterSpec.SetDatacenters(datacenters);

VM vmservice = serviceManager.VapiConnection.GetService<VM>();

List<VMTypes.Summary> vmsummarize = vmservice.List(VMFilterSpec);

if (vmsummarize.Count >= 0)
{
    string Vm_Id = vmsummarize[0].GetVm();
    if (Vm_Id != null)
    {
        Network netWork = serviceManager.VapiConnection.GetService<Network>();

            VMTypes.Info VMConfigInfo = vmservice.Get(Vm_Id);
            Dictionary<string, EthernetTypes.Info> dictOfEthernetAdapters = VMConfigInfo.GetNics();
            foreach (var item in dictOfEthernetAdapters)
            {
                EthernetTypes.Info NetworkDetails = item.Value;
                NetworkDetails.SetWakeOnLanEnabled(false);
                NetworkDetails.SetState(ConnectionState.UNRECOVERABLE_ERROR);
                NetworkDetails.SetStartConnected(false);
                NetworkDetails.SetAllowGuestControl(false);
            }
    }
}

1 个答案:

答案 0 :(得分:0)

最后,我一个人做。我发现要禁用NIC,必须先关闭电源然后再打开电源。感谢您向所有查看过的人查看我的问题。

VMTypes.PlacementSpec vmPlacementSpec = new VMTypes.PlacementSpec();

        VMTypes.FilterSpec VMFilterSpec = new VMTypes.FilterSpec();

        HashSet<string> datacenters = new HashSet<string>
        {
            GetDatacenter(serviceManager, vmModel.DataCenterName)
        };

        VMFilterSpec.SetNames(new HashSet<String> { vmModel.vmName });
        VMFilterSpec.SetDatacenters(datacenters);

        VM vmservice = serviceManager.VapiConnection.GetService<VM>();

        List<VMTypes.Summary> vmsummarize = vmservice.List(VMFilterSpec);

        if (vmsummarize.Count >= 0)
        {
            string Vm_Id = vmsummarize[0].GetVm();
            if (Vm_Id != null)
            {
                Power power = serviceManager.VapiConnection.GetService<Power>();
                Network netWork = serviceManager.VapiConnection.GetService<Network>();
                PowerTypes.Info powertype = power.Get(Vm_Id);

                if (powertype.GetState().Name == "POWERED_ON")
                {
                    Console.WriteLine("Power Off starting ");
                    power.Stop(Vm_Id);
                    Console.WriteLine("Powered Off Now ");

                    VMTypes.Info VMConfigInfo = vmservice.Get(Vm_Id);
                    Dictionary<string, EthernetTypes.Info> dictOfEthernetAdapters = VMConfigInfo.GetNics();
                    foreach (var item in dictOfEthernetAdapters)
                    {
                        EthernetTypes.Info NetworkDetails = item.Value;
                        NetworkDetails.SetWakeOnLanEnabled(false);
                        NetworkDetails.SetState(ConnectionState.UNRECOVERABLE_ERROR);
                        NetworkDetails.SetStartConnected(false);
                        NetworkDetails.SetAllowGuestControl(false);
                    }
                    Console.WriteLine("Power ON starting ");
                    power.Start(Vm_Id);
                    Console.WriteLine("Powered ON Now ");
                }
                else if (powertype.GetState().Name == "POWERED_OFF")
                {
                    VMTypes.Info VMConfigInfo = vmservice.Get(Vm_Id);
                    Dictionary<string, EthernetTypes.Info> dictOfEthernetAdapters = VMConfigInfo.GetNics();
                    foreach (var item in dictOfEthernetAdapters)
                    {
                        EthernetTypes.Info NetworkDetails = item.Value;
                        NetworkDetails.SetWakeOnLanEnabled(false);
                        NetworkDetails.SetState(ConnectionState.UNRECOVERABLE_ERROR);
                        NetworkDetails.SetStartConnected(false);
                        NetworkDetails.SetAllowGuestControl(false);
                    }
                    Console.WriteLine("Powered ON started ");
                    power.Start(Vm_Id);
                    Console.WriteLine("Powered ON Now ");
                }
            }
        }
        else
        {
            Console.WriteLine("Index was out of range");
        }