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); } } } } } }
public void SetVLan(ushort vLanId) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).DisposingUnless <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { using (IEthernetSwitchPortVlanSettingData portVlanSettingData1 = EthernetSwitchPortVlanSettingData.CreateEthernetSwitchPortVlanSettingData(this._Host)) { if (allocationSettingData == null) { this._Logger.Information("Nic not connected - no changes have been made to Vlan configuration."); } else if ((int)allocationSettingData.EnabledState == 3) { this._Logger.Information("Nic disabled - no changes have been made to Vlan configuration."); } else { using (IEthernetSwitchPortVlanSettingData portVlanSettingData2 = EthernetSwitchPortVlanSettingData.GetRelated(allocationSettingData).FirstOrDefault <IEthernetSwitchPortVlanSettingData>()) { if (portVlanSettingData2 == null) { portVlanSettingData1.AccessVlanId = vLanId; portVlanSettingData1.OperationMode = 1U; managmentService.AddFeatureSettings(allocationSettingData, new string[1] { ((IWMICommon)portVlanSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose())); } else { portVlanSettingData2.AccessVlanId = vLanId; portVlanSettingData2.OperationMode = 1U; managmentService.ModifyFeatureSettings(new string[1] { ((IWMICommon)portVlanSettingData2).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IEthernetSwitchFeatureSettingData>((Action <IEthernetSwitchFeatureSettingData>)(f => f.TryDispose())); } } } } } } } }