public void SetMacAddressType(MacAddressType macAddressType) { bool flag = false; switch (macAddressType) { case MacAddressType.Static: if (this._AdapterSettingData.Address != "000000000000") { flag = true; this._AdapterSettingData.StaticMacAddress = true; break; } break; case MacAddressType.Dynamic: flag = true; this._AdapterSettingData.StaticMacAddress = false; this._AdapterSettingData.Address = "000000000000"; break; } if (!flag) { return; } using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { IResourceAllocationSettingData rasd = managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)this._AdapterSettingData).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>(); this._AdapterSettingData.TryDispose(); this._AdapterSettingData = CommonEthernetPortSettingData.CreateFromResourceAllocationSettingData(rasd); } }
public void Disable() { 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>()) { if (allocationSettingData == null) { this._Logger.Information("Adapter disconnected."); } else { allocationSettingData.EnabledState = (ushort)3; string[] resourceSettings = new string[1] { ((IWMICommon)allocationSettingData).Object.GetText(TextFormat.WmiDtd20) }; managmentService.ModifyResourceSettings(resourceSettings).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(rasd => rasd.Dispose())); } } } } }
public void ConnectUsingID(string virtualSwitchID) { NIC.tracer.TraceInformation(string.Format("Connecting using ID {0}", (object)virtualSwitchID)); using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host)) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.Name == virtualSwitchID)).FirstOrDefault <IVirtualEthernetSwitch>()) { using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData1 == null) { NIC.tracer.TraceInformation(string.Format("Did not find existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID)); using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host)) { Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found"); Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchID + "' not found"); allocationSettingData2.Parent = ((IWMICommon)this._AdapterSettingData).Object.Path.Path; NIC.tracer.TraceInformation(string.Format("Switch port parent {0}", (object)allocationSettingData2.Parent)); allocationSettingData2.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; NIC.tracer.TraceInformation(string.Format("Switch port host resource {0}", (object[])allocationSettingData2.HostResource)); NIC.tracer.TraceInformation(string.Format("Adding resource settings to VM: {0}", (object)((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20))); managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose(); } } else { NIC.tracer.TraceInformation(string.Format("Found existing Ethernet port allocation setting data for {0}", (object)virtualSwitchID)); allocationSettingData1.EnabledState = (ushort)2; allocationSettingData1.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; NIC.tracer.TraceInformation(string.Format("Ethernet port allocation setting data for {0} set to {1}", (object)virtualSwitchID, (object)((IEnumerable <string>)allocationSettingData1.HostResource).FirstOrDefault <string>())); NIC.tracer.TraceInformation(string.Format("Modifying resource settings: {0}", (object)((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20))); managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose())); } } } } } } }
public void Connect(string virtualSwitchName) { using (VirtualEthernetSwitchManagementService.GetVirtualEthernetSwitchManagementService(this._Host)) { using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host)) { using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData()) { using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.GetAllVirtualEthernetSwitches(this._Host).DisposingUnless <IVirtualEthernetSwitch>((Func <IVirtualEthernetSwitch, bool>)(v => v.ElementName == virtualSwitchName)).FirstOrDefault <IVirtualEthernetSwitch>()) { using (IEthernetPortAllocationSettingData allocationSettingData1 = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>()) { if (allocationSettingData1 == null) { using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host)) { Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found"); Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found"); allocationSettingData2.Parent = ((IWMICommon)this._AdapterSettingData).Object.Path.Path; allocationSettingData2.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; managmentService.AddResourceSettings(systemSettingData, new string[1] { ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20) }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose(); } } else { Invariant.ArgumentNotNull((object)virtualEthernetSwitch, "Virtual switch '" + virtualSwitchName + "' not found"); allocationSettingData1.EnabledState = (ushort)2; allocationSettingData1.HostResource = new string[1] { ((IWMICommon)virtualEthernetSwitch).Object.Path.Path }; managmentService.ModifyResourceSettings(new string[1] { ((IWMICommon)allocationSettingData1).Object.GetText(TextFormat.WmiDtd20) }).ForEach <IResourceAllocationSettingData>((Action <IResourceAllocationSettingData>)(v => v.Dispose())); } } } } } } }