Ejemplo n.º 1
0
 public NetworkAdapterInfo GetNetworkAdapterInfo()
 {
     using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
     {
         using (IEthernetPortAllocationSettingData epasd = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
         {
             if (epasd == null)
             {
                 return new NetworkAdapterInfo()
                        {
                            SwitchPort = new SwitchPortInfo()
                            {
                                Name = string.Empty
                            },
                            VirtualSwitch = new VirtualSwitchInfo()
                            {
                                Label = string.Empty
                            }
                        }
             }
             ;
             string vsPath = epasd.HostResource == null || epasd.HostResource.Length == 0 ? string.Empty : epasd.HostResource[0];
             using (IEthernetSwitchPortVlanSettingData portVlanSettingData = EthernetSwitchPortVlanSettingData.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPortVlanSettingData>())
             {
                 using (IEthernetSwitchPort ethernetSwitchPort = EthernetSwitchPort.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPort>())
                 {
                     using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => ((IWMICommon)v).Object.Path.Path == vsPath)).FirstOrDefault <IVirtualEthernetSwitch>())
                     {
                         NetworkAdapterInfo networkAdapterInfo = new NetworkAdapterInfo()
                         {
                             SwitchPort    = new SwitchPortInfo(),
                             VirtualSwitch = new VirtualSwitchInfo()
                         };
                         if (ethernetSwitchPort == null)
                         {
                             if (virtualEthernetSwitch != null)
                             {
                                 networkAdapterInfo.SwitchPort.Name = virtualEthernetSwitch.ElementName;
                                 networkAdapterInfo.SwitchPort.Guid = virtualEthernetSwitch.Name;
                             }
                         }
                         else
                         {
                             networkAdapterInfo.SwitchPort.Name = ethernetSwitchPort.SystemName;
                             networkAdapterInfo.SwitchPort.Guid = ethernetSwitchPort.Name;
                         }
                         networkAdapterInfo.SwitchPort.AccessVLanId = portVlanSettingData == null ? 0 : (int)portVlanSettingData.AccessVlanId;
                         if (virtualEthernetSwitch != null)
                         {
                             networkAdapterInfo.VirtualSwitch.Label      = virtualEthernetSwitch.ElementName;
                             networkAdapterInfo.VirtualSwitch.SwitchUuid = virtualEthernetSwitch.Name;
                         }
                         return(networkAdapterInfo);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public void RemoveVLan()
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
         {
             using (IEthernetPortAllocationSettingData epasd = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
             {
                 if (epasd == null)
                 {
                     this._Logger.Information("Nic already not connected - no changes have been made to Vlan configuration.");
                 }
                 else if ((int)epasd.EnabledState == 3)
                 {
                     this._Logger.Information("Nic disabled - no changes have been made to Vlan configuration.");
                 }
                 else
                 {
                     using (IEthernetSwitchPortVlanSettingData portVlanSettingData = EthernetSwitchPortVlanSettingData.GetRelated(epasd).FirstOrDefault <IEthernetSwitchPortVlanSettingData>())
                     {
                         if (portVlanSettingData == null)
                         {
                             this._Logger.Information("No Vlan has been specified - no changes have been made to Vlan configuration.");
                         }
                         else
                         {
                             portVlanSettingData.AccessVlanId  = (ushort)0;
                             portVlanSettingData.OperationMode = 1U;
                             managmentService.ModifyFeatureSettings(new string[1]
                             {
                                 ((IWMICommon)portVlanSettingData).Object.GetText(TextFormat.WmiDtd20)
                             }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose()));
                         }
                     }
                 }
             }
         }
     }
 }