Search code examples
c#apivmwarevspherenic

Issue in Disable Network interface Card of VM from the VMWare using vsphere-automation-sdk.net in c#


Here is my code to disable network interface card of VM in the VMware. It works perfectly and there is no exception all credentials are correct. It is coded as same as mentioned in sdk documentation. But after running when i see the VM on VMWare there NIC is not disabled. Any one please help me to fix this. Thank you.

Basically, We prvide to our client DRaaS. So for DR test we are trying to create a new VM clone of existing VM on the same VMWare. But when we make clone of VM its NIC conflicts with the existing VM. Thank you.

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);
            }
    }
}

Solution

  • Finally i did it by myself. I found for disable NIC, I have to powered off and then Powered on. Thanks for viewing my question to all who viewed.

    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");
            }