Ejemplo n.º 1
0
 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()));
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public void Disconnect()
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualSystemSettingData systemSettingData = this._Vm.GetVirtualSystemSettingData())
         {
             using (IEthernetPortAllocationSettingData allocationSettingData = EthernetPortAllocationSettingData.GetRelated(systemSettingData).Where <IEthernetPortAllocationSettingData>((Func <IEthernetPortAllocationSettingData, bool>)(e => e.Parent == ((IWMICommon)this._AdapterSettingData).Object.Path.Path)).FirstOrDefault <IEthernetPortAllocationSettingData>())
             {
                 if (allocationSettingData == null)
                 {
                     NIC.tracer.TraceInformation("Did not find connection to remove.");
                 }
                 else
                 {
                     using (IResourceAllocationSettingData fromObject = ResourceAllocationSettingData.CreateFromObject(((IWMICommon)allocationSettingData).Object))
                     {
                         NIC.tracer.TraceInformation("Found connection to remove.");
                         managmentService.RemoveResourceSettings(new IResourceAllocationSettingData[1]
                         {
                             fromObject
                         });
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 3
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.º 4
0
 public void DestroySnapshot(IVirtualSystemSettingData affectedSnapshot)
 {
     using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(DestroySnapshot)))
     {
         methodParameters["AffectedSnapshot"] = (object)affectedSnapshot.Object;
         this._Instance.InvokeAndWait(nameof(DestroySnapshot), methodParameters).TryDispose();
     }
 }
Ejemplo n.º 5
0
 public void DestroySnapshotTree(IVirtualSystemSettingData snapshotSettingData)
 {
     using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(DestroySnapshotTree)))
     {
         methodParameters["SnapshotSettingData"] = (object)snapshotSettingData.Object;
         this._Instance.InvokeAndWait(nameof(DestroySnapshotTree), methodParameters).TryDispose();
     }
 }
Ejemplo n.º 6
0
        public static IVirtualSystemSettingData GetVirtualSystemSettingDataByName(ManagementScope scope, string name)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.Query(scope, "ElementName='{0}'", (object)name).Where <IVirtualSystemSettingData>((Func <IVirtualSystemSettingData, bool>)(vssd => vssd.VirtualSystemType == "Microsoft:Hyper-V:System:Realized")).FirstOrDefault <IVirtualSystemSettingData>();

            if (systemSettingData != null)
            {
                return(systemSettingData);
            }
            throw new HyperVException("VirtualSystemSettingData not found for VM name " + name);
        }
Ejemplo n.º 7
0
        public static IVirtualSystemSettingData GetVirtualSystemSettingDataByVmUuid(ManagementScope scope, string vmUuid)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.Query(scope, "VirtualSystemIdentifier='{0}'", (object)vmUuid).Where <IVirtualSystemSettingData>((Func <IVirtualSystemSettingData, bool>)(vssd => vssd.VirtualSystemType == "Microsoft:Hyper-V:System:Realized")).FirstOrDefault <IVirtualSystemSettingData>();

            if (systemSettingData != null)
            {
                return(systemSettingData);
            }
            throw new HyperVException("VirtualSystemSettingData not found for VM uuid " + vmUuid);
        }
Ejemplo n.º 8
0
 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()));
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
        public IVm CreateMinimalVm(string name, string location)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(this._Host);

            systemSettingData.ElementName           = name;
            systemSettingData.ConfigurationDataRoot = location;
            systemSettingData.SnapshotDataRoot      = location;
            List <string> stringList = new List <string>();

            using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
                return((IVm) new Vm(this._Host, managmentService.DefineSystem(systemSettingData, stringList.ToArray(), (IVirtualSystemSettingData)null), this._Logger));
        }
Ejemplo n.º 10
0
        public static INIC Create(IComputerSystem host, IComputerSystem vm, VirtualNicType nicType, string nicGuid, ILogger logger)
        {
            ICommonEthernetPortSettingData ethernetPortSettingData;

            switch (nicType)
            {
            case VirtualNicType.Default:
            case VirtualNicType.Legacy:
                ethernetPortSettingData             = (ICommonEthernetPortSettingData)EmulatedEthernetPortSettingData.GetDefaultLegacyAdapter(host);
                ethernetPortSettingData.ElementName = "Legacy Network Adapter";
                break;

            case VirtualNicType.Synthetic:
                ethernetPortSettingData = (ICommonEthernetPortSettingData)SyntheticEthernetPortSettingData.GetDefaultSyntheticAdapter(host);
                ((ISyntheticEthernetPortSettingData)ethernetPortSettingData).VirtualSystemIdentifiers = new string[1]
                {
                    nicGuid
                };
                ethernetPortSettingData.ElementName = "Synthetic Network Adapter";
                break;

            default:
                throw new HyperVException("I don't know about the nic type " + (object)nicType);
            }
            ethernetPortSettingData.StaticMacAddress = false;
            using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(host))
            {
                using (IVirtualSystemSettingData systemSettingData = vm.GetVirtualSystemSettingData())
                {
                    IResourceAllocationSettingData adapter = managmentService.AddResourceSettings(systemSettingData, new string[1]
                    {
                        ((IWMICommon)ethernetPortSettingData).Object.GetText(TextFormat.WmiDtd20)
                    }).FirstOrDefault <IResourceAllocationSettingData>();
                    try
                    {
                        ICommonEthernetPortSettingData adapterSettingData = CommonEthernetPortSettingData.GetRelated(systemSettingData).Where <ICommonEthernetPortSettingData>((Func <ICommonEthernetPortSettingData, bool>)(cepsd => adapter.InstanceID == cepsd.InstanceID)).FirstOrDefault <ICommonEthernetPortSettingData>();
                        if (adapterSettingData == null)
                        {
                            throw new HyperVException("Couldn't find the network adapter we just created");
                        }
                        return((INIC) new NIC(host, vm, adapterSettingData, logger));
                    }
                    finally
                    {
                        if (adapter != null)
                        {
                            adapter.Dispose();
                        }
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public IVm CreateVm(VmCreationInfo info)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(this._Host);

            systemSettingData.ElementName           = info.Name;
            systemSettingData.ConfigurationDataRoot = info.Location;
            systemSettingData.SnapshotDataRoot      = info.Location;
            systemSettingData.BootOrder             = new ushort[4]
            {
                (ushort)2,
                (ushort)1,
                (ushort)0,
                (ushort)3
            };
            if (!string.IsNullOrEmpty(info.VirtualSystemSubType) && info.VirtualSystemSubType != "Microsoft:Hyper-V:SubType:1")
            {
                systemSettingData.VirtualSystemSubType = info.VirtualSystemSubType;
            }
            List <string> stringList = new List <string>();

            using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
            {
                IVm vm = (IVm) new Vm(this._Host, managmentService.DefineSystem(systemSettingData, stringList.ToArray(), (IVirtualSystemSettingData)null), this._Logger);
                vm.CPUCount         = (ulong)info.NumCPU;
                vm.CPUCompatibility = info.CPUCompatibility;
                if (info.MemoryMB % 2L == 1L)
                {
                    ++info.MemoryMB;
                }
                vm.MemorySize = (ulong)(info.MemoryMB * 1024L) * 1024UL;
                try
                {
                    foreach (HVNicInfo nic in info.Nics)
                    {
                        if (string.Compare(nic.VirtualNetwork, "---Discard---", true, CultureInfo.InvariantCulture) != 0)
                        {
                            NicConnectionStatus nicStatus = string.IsNullOrEmpty(nic.VirtualNetwork) ? NicConnectionStatus.Disconnected : NicConnectionStatus.Connected;
                            vm.AddNic(nic.NicType, nic.VirtualNetwork, nicStatus, nic.NicGuid, nic.vLan);
                        }
                    }
                }
                catch (Exception ex)
                {
                    this._Logger.FormatErrorWithException(ex, "Failed to provision the NICs for the vm.  Removing the vm");
                    using (vm)
                        managmentService.DestroySystem(((IVmInternals)vm).ComputerSystem);
                    throw;
                }
                return(vm);
            }
        }
Ejemplo n.º 12
0
 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()));
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 13
0
 public ISummaryInformation GetSummaryInformation(string vmName)
 {
     using (IVirtualSystemSettingData settingDataByName = VirtualSystemSettingData.GetVirtualSystemSettingDataByName(this._Instance.Scope, vmName))
     {
         if (settingDataByName == null)
         {
             throw new HyperVmNotFoundException(vmName);
         }
         ManagementObject[] managementObjectArray = new ManagementObject[1]
         {
             settingDataByName.ManagementObject
         };
         using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(GetSummaryInformation)))
         {
             uint[] numArray = new uint[17]
             {
                 1U,
                 2U,
                 3U,
                 4U,
                 8U,
                 100U,
                 103U,
                 104U,
                 105U,
                 106U,
                 107U,
                 109U,
                 110U,
                 112U,
                 113U,
                 123U,
                 132U
             };
             methodParameters["SettingData"]          = (object)managementObjectArray;
             methodParameters["RequestedInformation"] = (object)numArray;
             using (ManagementBaseObject managementBaseObject = this._Instance.InvokeAndWait(nameof(GetSummaryInformation), methodParameters))
             {
                 if ((int)(uint)managementBaseObject["ReturnValue"] == 0)
                 {
                     return((ISummaryInformation) new SummaryInformation(((ManagementBaseObject[])managementBaseObject["SummaryInformation"])[0]));
                 }
             }
             throw new HyperVException("Failed to retrieve SummaryInformation for VM " + vmName);
         }
     }
 }
Ejemplo n.º 14
0
 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()));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 15
0
        public IEnumerable <IResourceAllocationSettingData> AddResourceSettings(IVirtualSystemSettingData affectedConfiguration, string[] resourceSettings)
        {
            ManagementBaseObject inParams = this._Instance.GetMethodParameters(nameof(AddResourceSettings));

            try
            {
                inParams["AffectedConfiguration"] = (object)affectedConfiguration.Object.Path.Path;
                inParams["ResourceSettings"]      = (object)resourceSettings;
                ManagementBaseObject result = this._Instance.InvokeAndWait(nameof(AddResourceSettings), inParams);
                try
                {
                    if (result["ResultingResourceSettings"] != null)
                    {
                        string[] strArray = (string[])result["ResultingResourceSettings"];
                        for (int index = 0; index < strArray.Length; ++index)
                        {
                            yield return(ResourceAllocationSettingData.CreateFromPath(strArray[index], this.Scope));
                        }
                        strArray = (string[])null;
                    }
                    else if (result["Job"] != null)
                    {
                        foreach (ManagementObject o in ((ManagementObject)result["Job"]).GetRelated((string)null, "Msvm_AffectedJobElement", (string)null, (string)null, (string)null, (string)null, false, (EnumerationOptions)null))
                        {
                            yield return(ResourceAllocationSettingData.CreateFromObject(o));
                        }
                    }
                }
                finally
                {
                    if (result != null)
                    {
                        result.Dispose();
                    }
                }
                result = (ManagementBaseObject)null;
            }
            finally
            {
                if (inParams != null)
                {
                    inParams.Dispose();
                }
            }
            inParams = (ManagementBaseObject)null;
        }
Ejemplo n.º 16
0
 public static IMemorySettingData GetRelated(IComputerSystem vm)
 {
     using (IVirtualSystemSettingData systemSettingData = vm.GetVirtualSystemSettingData())
     {
         using (ManagementObjectCollection related = systemSettingData.Object.GetRelated("Msvm_MemorySettingData"))
         {
             using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = related.GetEnumerator())
             {
                 if (enumerator.MoveNext())
                 {
                     return((IMemorySettingData) new MemorySettingData((ManagementObject)enumerator.Current));
                 }
             }
         }
     }
     throw new HyperVException("No related Msvm_MemorySettingData objects found");
 }
        public static IEnumerable <ICommonEthernetPortSettingData> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection adapters = vssd.Object.GetRelated("CIM_EthernetPortAllocationSettingData");

            try
            {
                foreach (ManagementObject managementObject in adapters)
                {
                    ManagementObject adapter = managementObject;
                    if ((int)(ushort)adapter["ResourceType"] != 10)
                    {
                        adapter.Dispose();
                    }
                    else
                    {
                        string adapterClass = (string)adapter["__Class"];
                        if (string.Equals(adapterClass, "Msvm_SyntheticEthernetPortSettingData", StringComparison.InvariantCultureIgnoreCase))
                        {
                            yield return(SyntheticEthernetPortSettingData.CreateFromManagementObject(adapter));
                        }
                        else
                        {
                            if (!string.Equals(adapterClass, "Msvm_EmulatedEthernetPortSettingData", StringComparison.InvariantCultureIgnoreCase))
                            {
                                throw new HyperVException("I don't know how to handle an Ethernet port setting data of type " + adapterClass);
                            }
                            yield return(EmulatedEthernetPortSettingData.CreateFromManagementObject(adapter));
                        }
                        adapterClass = (string)null;
                    }
                    adapter = (ManagementObject)null;
                }
            }
            finally
            {
                if (adapters != null)
                {
                    adapters.Dispose();
                }
            }
            adapters = (ManagementObjectCollection)null;
        }
        public static IEnumerable <IStorageAllocationSettingData> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection sasds = vssd.Object.GetRelated("Msvm_StorageAllocationSettingData", "Msvm_VirtualSystemSettingDataComponent", (string)null, (string)null, (string)null, (string)null, false, (EnumerationOptions)null);

            try
            {
                foreach (ManagementObject instance in sasds)
                {
                    yield return((IStorageAllocationSettingData) new StorageAllocationSettingData(instance));
                }
            }
            finally
            {
                if (sasds != null)
                {
                    sasds.Dispose();
                }
            }
            sasds = (ManagementObjectCollection)null;
        }
        public static IEnumerable <IResourceAllocationSettingData> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection rasds = vssd.Object.GetRelated("Msvm_ResourceAllocationSettingData");

            try
            {
                foreach (ManagementObject instance in rasds)
                {
                    yield return((IResourceAllocationSettingData) new ResourceAllocationSettingData(instance));
                }
            }
            finally
            {
                if (rasds != null)
                {
                    rasds.Dispose();
                }
            }
            rasds = (ManagementObjectCollection)null;
        }
Ejemplo n.º 20
0
        public static IEnumerable <IVirtualEthernetSwitch> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection virtSwitches = vssd.Object.GetRelated("Msvm_VirtualEthernetSwitch");

            try
            {
                foreach (ManagementObject instance in virtSwitches)
                {
                    yield return((IVirtualEthernetSwitch) new VirtualEthernetSwitch(instance));
                }
            }
            finally
            {
                if (virtSwitches != null)
                {
                    virtSwitches.Dispose();
                }
            }
            virtSwitches = (ManagementObjectCollection)null;
        }
Ejemplo n.º 21
0
        public static IEnumerable <IEthernetPortAllocationSettingData> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection epasds = vssd.Object.GetRelated("Msvm_EthernetPortAllocationSettingData");

            try
            {
                foreach (ManagementObject instance in epasds)
                {
                    yield return((IEthernetPortAllocationSettingData) new EthernetPortAllocationSettingData(instance));
                }
            }
            finally
            {
                if (epasds != null)
                {
                    epasds.Dispose();
                }
            }
            epasds = (ManagementObjectCollection)null;
        }
Ejemplo n.º 22
0
        public static IEnumerable <INIC> GetRelated(IComputerSystem host, IComputerSystem vm, ILogger logger)
        {
            IVirtualSystemSettingData vssd = vm.GetVirtualSystemSettingData();

            try
            {
                foreach (ICommonEthernetPortSettingData adapterSettingData in CommonEthernetPortSettingData.GetRelated(vssd))
                {
                    yield return((INIC) new NIC(host, vm, adapterSettingData, logger));
                }
            }
            finally
            {
                if (vssd != null)
                {
                    vssd.Dispose();
                }
            }
            vssd = (IVirtualSystemSettingData)null;
        }
        public new static IEnumerable <ICommonEthernetPortSettingData> GetRelated(IVirtualSystemSettingData vssd)
        {
            ManagementObjectCollection adapters = vssd.Object.GetRelated("Msvm_EmulatedEthernetPortSettingData");

            try
            {
                foreach (ManagementObject instance in adapters)
                {
                    yield return((ICommonEthernetPortSettingData) new EmulatedEthernetPortSettingData(instance));
                }
            }
            finally
            {
                if (adapters != null)
                {
                    adapters.Dispose();
                }
            }
            adapters = (ManagementObjectCollection)null;
        }
Ejemplo n.º 24
0
 public IComputerSystem DefineSystem(string systemSettings, string[] resourceSettings, IVirtualSystemSettingData referenceConfiguration)
 {
     using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(DefineSystem)))
     {
         methodParameters["SystemSettings"]   = (object)systemSettings;
         methodParameters["ResourceSettings"] = (object)resourceSettings;
         if (referenceConfiguration != null)
         {
             methodParameters["ReferenceConfiguration"] = (object)referenceConfiguration.Object;
         }
         using (ManagementBaseObject managementBaseObject = this._Instance.InvokeAndWait(nameof(DefineSystem), methodParameters))
         {
             using (ManagementObject managementObject = new ManagementObject((string)managementBaseObject["ResultingSystem"]))
                 return(ComputerSystem.GetComputerSystem(this._Instance.Scope, (string)managementObject["ElementName"]));
         }
     }
 }
 public IVirtualEthernetSwitch DefineSystem(IVirtualEthernetSwitchSettingData systemSettings, IEnumerable <IEthernetPortAllocationSettingData> resourceSettings, IVirtualSystemSettingData referenceConfiguration)
 {
     using (ManagementBaseObject methodParameters = this._Instance.GetMethodParameters(nameof(DefineSystem)))
     {
         methodParameters["SystemSettings"]   = (object)((IWMICommon)systemSettings).Object.GetText(TextFormat.WmiDtd20);
         methodParameters["ResourceSettings"] = (object)resourceSettings.Select <IEthernetPortAllocationSettingData, string>((Func <IEthernetPortAllocationSettingData, string>)(rs => ((IWMICommon)rs).Object.GetText(TextFormat.WmiDtd20))).ToArray <string>();
         using (ManagementBaseObject managementBaseObject = this._Instance.InvokeAndWait(nameof(DefineSystem), methodParameters))
             return(VirtualEthernetSwitch.CreateFromManagementObject(new ManagementObject((string)managementBaseObject["ResultingSystem"])));
     }
 }
Ejemplo n.º 26
0
 protected void AddNicToVm(IVm vm, VirtualNicType nicType, string vswitchName, NicConnectionStatus nicStatus, string nicGuid, int VLan)
 {
     using (IVirtualSystemManagementService managmentService = VirtualSystemManagementService.GetVirtualSystemManagmentService(this._Host))
     {
         using (IVirtualEthernetSwitch virtualEthernetSwitch = VirtualEthernetSwitch.Query(this._Host, "ElementName='{0}'", (object)vswitchName).FirstOrDefault <IVirtualEthernetSwitch>())
         {
             if (virtualEthernetSwitch == null)
             {
                 throw new HyperVException("Virtual Ethernet switch '" + vswitchName + "' not found");
             }
             using (IVirtualSystemSettingData systemSettingData = ((IVmInternals)vm).ComputerSystem.GetVirtualSystemSettingData())
             {
                 ICommonEthernetPortSettingData ethernetPortSettingData;
                 if (nicType != VirtualNicType.Legacy)
                 {
                     if (nicType != VirtualNicType.Synthetic)
                     {
                         throw new HyperVException("I don't know about the nic type " + nicType.ToString());
                     }
                     ethernetPortSettingData = (ICommonEthernetPortSettingData)SyntheticEthernetPortSettingData.GetDefaultSyntheticAdapter(this._Host);
                     ((ISyntheticEthernetPortSettingData)ethernetPortSettingData).VirtualSystemIdentifiers = new string[1]
                     {
                         nicGuid
                     };
                     ethernetPortSettingData.ElementName = "Synthetic Network Adapter";
                 }
                 else
                 {
                     ethernetPortSettingData             = (ICommonEthernetPortSettingData)EmulatedEthernetPortSettingData.GetDefaultLegacyAdapter(this._Host);
                     ethernetPortSettingData.ElementName = "Legacy Network Adapter";
                 }
                 using (ethernetPortSettingData)
                 {
                     ethernetPortSettingData.StaticMacAddress = false;
                     using (IResourceAllocationSettingData allocationSettingData1 = managmentService.AddResourceSettings(systemSettingData, new string[1]
                     {
                         ((IWMICommon)ethernetPortSettingData).Object.GetText(TextFormat.WmiDtd20)
                     }).FirstOrDefault <IResourceAllocationSettingData>())
                     {
                         if (nicStatus != NicConnectionStatus.Connected)
                         {
                             return;
                         }
                         using (IEthernetPortAllocationSettingData allocationSettingData2 = EthernetPortAllocationSettingData.GetDefaultEthernetPortAllocationSettingData(this._Host))
                         {
                             using (IVirtualSwitch virtualSwitchByName = this.GetVirtualSwitchByName(vswitchName))
                             {
                                 Invariant.ArgumentNotNull((object)allocationSettingData2, "Switch port not found");
                                 Invariant.ArgumentNotNull((object)virtualSwitchByName, "Virtual switch '" + vswitchName + "' not found");
                                 allocationSettingData2.Parent       = ((IWMICommon)allocationSettingData1).Object.Path.Path;
                                 allocationSettingData2.HostResource = new string[1]
                                 {
                                     virtualSwitchByName.Object.Path.Path
                                 };
                                 managmentService.AddResourceSettings(systemSettingData, new string[1]
                                 {
                                     ((IWMICommon)allocationSettingData2).Object.GetText(TextFormat.WmiDtd20)
                                 }).FirstOrDefault <IResourceAllocationSettingData>().TryDispose();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 27
0
 public IComputerSystem DefineSystem(IVirtualSystemSettingData systemSettings, string[] resourceSettings, IVirtualSystemSettingData referenceConfiguration)
 {
     return(this.DefineSystem(systemSettings.Object.GetText(TextFormat.WmiDtd20), resourceSettings, referenceConfiguration));
 }
Ejemplo n.º 28
0
        public static IVirtualSystemSettingData CreateFromExisting(IComputerSystem host, IVirtualSystemSettingData vssd, string location)
        {
            IVirtualSystemSettingData systemSettingData = VirtualSystemSettingData.CreateVirtualSystemSettingData(host);
            List <string>             stringList        = new List <string>()
            {
                "ElementName",
                "ConfigurationDataRoot",
                "SnapshotDataRoot",
                "AdditionalRecoveryInformation",
                "AllowFullSCSICommandSet",
                "AllowReducedFcRedundancy",
                "AutomaticRecoveryAction",
                "AutomaticShutdownAction",
                "AutomaticStartupAction",
                "AutomaticStartupActionDelay",
                "AutomaticStartupActionSequenceNumber",
                "BaseBoardSerialNumber",
                "BIOSGUID",
                "BIOSNumLock",
                "BIOSSerialNumber",
                "BootOrder",
                "ChassisAssetTag",
                "ChassisSerialNumber",
                "ConfigurationFile",
                "ConfigurationID",
                "DebugChannelId",
                "DebugPort",
                "DebugPortEnabled",
                "IncrementalBackupEnabled",
                "IsSaved",
                "LogDataRoot",
                "LowMmioGapSize",
                "Notes",
                "PauseAfterBootFailure",
                "SecureBootEnabled",
                "SuspendDataRoot",
                "SwapFileDataRoot",
                "Version",
                "VirtualNumaEnabled",
                "VirtualSystemType",
                "VirtualSystemSubType"
            };

            if (vssd.VirtualSystemSubType != "Microsoft:Hyper-V:SubType:1")
            {
                stringList.Add("NetworkBootPreferredProtocol");
            }
            ManagementObject managementObject1 = vssd.Object;
            ManagementObject managementObject2 = systemSettingData.Object;

            foreach (string index in stringList)
            {
                try
                {
                    managementObject2[index] = managementObject1[index];
                }
                catch (Exception ex)
                {
                }
            }
            systemSettingData.ConfigurationDataRoot = location;
            systemSettingData.SnapshotDataRoot      = location;
            systemSettingData.SuspendDataRoot       = location;
            systemSettingData.SwapFileDataRoot      = location;
            return(systemSettingData);
        }
Ejemplo n.º 29
0
 public ulong GetSizeOfSystemFiles(IVirtualSystemSettingData vmSettingData)
 {
     throw new NotImplementedException(nameof(GetSizeOfSystemFiles));
 }
Ejemplo n.º 30
0
 public byte[] GetVirtualSystemThumbnailImage(IVirtualSystemSettingData vmSettingData, ushort widthPixels, ushort heightPixels)
 {
     throw new NotImplementedException(nameof(GetVirtualSystemThumbnailImage));
 }