internal ManagementObject CreateResource(Resources resource) { string resourcePoolClass = "Msvm_ResourcePool"; if (resource == Resources.Processor) { resourcePoolClass = "Msvm_ProcessorPool"; } string resourceSubType = ""; switch (resource) { case Resources.Processor: resourceSubType = "Microsoft:Hyper-V:Processor"; break; case Resources.Memory: resourceSubType = "Microsoft:Hyper-V:Memory"; break; case Resources.SCSIController: resourceSubType = "Microsoft:Hyper-V:Synthetic SCSI Controller"; break; case Resources.VirtualHardDrive: resourceSubType = "Microsoft:Hyper-V:Synthetic Disk Drive"; break; case Resources.VirtualHardDisk: resourceSubType = "Microsoft:Hyper-V:Virtual Hard Disk"; break; case Resources.VirtualDvdDrive: resourceSubType = "Microsoft:Hyper-V:Synthetic DVD Drive"; break; case Resources.VirtualDvdDisk: resourceSubType = "Microsoft:Hyper-V:Virtual CD/DVD Disk"; break; case Resources.NetworkAdapter: resourceSubType = "Microsoft:Hyper-V:Synthetic Ethernet Port"; break; case Resources.SwitchPort: resourceSubType = "Microsoft:Hyper-V:Ethernet Connection"; break; } string defaultSettingPath = null; ObjectQuery query = new ObjectQuery($"SELECT * FROM {resourcePoolClass} WHERE ResourceSubType = \"{resourceSubType}\" AND Primordial = True"); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(virtualizationScope, query)) using (ManagementObject resourcePool = searcher.Get().First()) using (ManagementObjectCollection capabilitiesCollection = resourcePool.GetRelated("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", null, null, null, null, false, null)) using (ManagementObject capabilities = capabilitiesCollection.First()) foreach (ManagementObject settingAssociation in capabilities.GetRelationships("Msvm_SettingsDefineCapabilities")) { if ((ushort)settingAssociation["ValueRole"] == 0) { defaultSettingPath = (string)settingAssociation["PartComponent"]; break; } } if (defaultSettingPath == null) { throw new ManagementException("Unable to find the Default Resource Settings."); } using (ManagementObject defaultSetting = new ManagementObject(defaultSettingPath)) { defaultSetting.Scope = virtualizationScope; defaultSetting.Get(); return(defaultSetting); } }
private void PrintObject(ManagementScope scope, string path) { var site = new ManagementObject(scope, new ManagementPath(path), null); object desc = site["Description"]; foreach (var rel in site.GetRelationships()) { Console.WriteLine("rel: " + rel); } foreach (var rel in site.GetRelated()) { Console.WriteLine("related: " + rel); } Console.WriteLine("props:"); foreach (var prop in site.Properties) { Console.WriteLine(" {0}: {1}", prop.Name, prop.Value); } Console.WriteLine("system props:"); foreach (var prop in site.SystemProperties) { Console.WriteLine(" {0}: {1}", prop.Name, prop.Value); } }
public static IEmulatedEthernetPortSettingData GetDefaultFromResourcePool(IResourcePool resource) { ManagementObject managementObject = (ManagementObject)null; using (ManagementObjectCollection related = ((IWMICommon)resource).Object.GetRelated("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", (string)null, (string)null, (string)null, (string)null, false, (EnumerationOptions)null)) { using (ManagementObject firstElement = related.GetFirstElement()) { foreach (ManagementObject relationship in firstElement.GetRelationships("Msvm_SettingsDefineCapabilities")) { if ((int)(ushort)relationship["ValueRole"] == 0) { managementObject = relationship; break; } relationship.Dispose(); } } } if (managementObject == null) { throw new HyperVException("Unable to find the default settings for the resource"); } string path = (string)managementObject["PartComponent"]; managementObject.Dispose(); ManagementObject instance = new ManagementObject(path); instance.Scope = ((IWMICommon)resource).Scope; instance.Get(); return((IEmulatedEthernetPortSettingData) new EmulatedEthernetPortSettingData(instance)); }
GetDefaultObjectFromResourcePool( ManagementObject resourcePool, ManagementScope scope) { // // The default object is associated with the Msvm_AllocationCapabilities object that // is associated to the resource pool. // ManagementObject defaultSettingAssociation = null; using (ManagementObjectCollection capabilitiesCollection = resourcePool.GetRelated("Msvm_AllocationCapabilities", "Msvm_ElementCapabilities", null, null, null, null, false, null)) using (ManagementObject capabilities = WmiUtilities.GetFirstObjectFromCollection(capabilitiesCollection)) { foreach (ManagementObject settingAssociation in capabilities.GetRelationships("Msvm_SettingsDefineCapabilities")) { if ((ushort)settingAssociation["ValueRole"] == 0) { defaultSettingAssociation = settingAssociation; break; } else { settingAssociation.Dispose(); } } } if (defaultSettingAssociation == null) { throw new ManagementException("Unable to find the default settings!"); } string defaultSettingPath = (string)defaultSettingAssociation["PartComponent"]; defaultSettingAssociation.Dispose(); ManagementObject defaultSetting = new ManagementObject(defaultSettingPath); defaultSetting.Scope = scope; defaultSetting.Get(); return(defaultSetting); }
internal static ManagementObject GetDefaultRelationshipClass(this ManagementObject allocationCapabilities) { return(allocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities") .OfType <ManagementObject>() .FirstOrDefault(x => (UInt16)x["ValueRole"] == 0)); }
// // METHODS // // <doc> // <desc> // Create associator nodes under this node. // The children of this node are of type WMIAssocGroupNode. // Each WMIAssocGroupNode groups instances that belong to the same class; // are related to this one through the same association class, // and play the same role. // </desc> // </doc> public override Node[] CreateChildren() { try { //do not look for associators of a new instance if (this.IsNewInstance) { return(null); } //BUGBUG: wmi raid 2855 //for Beta1, we are going to disable the ability to expand //references of Win32_ComputerSystem if (className.ToUpper() == "WIN32_COMPUTERSYSTEM" && nsName.ToUpper() == "ROOT\\CIMV2") { return(null); } //First, get association instances related to this GetRelationshipOptions opts = new GetRelationshipOptions(null, //context TimeSpan.MaxValue, //timeout 50, //block size false, //rewindable true, //return immediately true, //use amended true, //locatable false, //prototype only false, //direct read string.Empty, //RELATIONSHIP CLASS string.Empty, //relationship qualifier string.Empty, //this role false //classes only ); ManagementObjectCollection assocInstances = mgmtObj.GetRelationships(opts); if (assocInstances == null) { return(null); } //assocGroups will contain a list of unique association/class/role groupings ArrayList assocGroups = new ArrayList(); ManagementObjectCollection.ManagementObjectEnumerator enumAssocInstances = assocInstances.GetEnumerator(); while (enumAssocInstances.MoveNext()) { ManagementObject curObj = (ManagementObject)enumAssocInstances.Current; AssocGroupComponent comp = new AssocGroupComponent(curObj, mgmtObj); if (!assocGroups.Contains(comp)) //this relies on our implementation of "Equald" in AssocGroupComponent class { assocGroups.Add(comp); } } Node[] childNodes = new Node[assocGroups.Count]; Object[] arAssocGroups = assocGroups.ToArray(); for (int i = 0; i < arAssocGroups.Length; i++) { childNodes[i] = new WMIAssocGroupNode((AssocGroupComponent)arAssocGroups[i], connectAs, password); } return(childNodes); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); return(null); } }