CreateExternalSwitch(
            string externalAdapterName,
            string switchName,
            string switchNotes)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            string[] ports;

            //
            // Get the external adapter we are connecting to.
            //
            using (ManagementObject externalAdapter =
                       NetworkingUtilities.FindExternalAdapter(externalAdapterName, scope))

                //
                // Get the hosting computer system. The internal port we create needs to be configured
                // to connect to the hosting computer system.
                //
                using (ManagementObject hostComputerSystem = WmiUtilities.GetHostComputerSystem(scope))

                    //
                    // Get the default Msvm_EthernetPortAllocationSettingData instance that we can use to
                    // configure our external port connection for the switch.
                    // Use the same switch name, appended with "_External", for the port name.
                    // You can use any port name that you like.
                    //
                    using (ManagementObject externalPortToCreate =
                               NetworkingUtilities.GetDefaultEthernetPortAllocationSettingData(scope))
                    {
                        externalPortToCreate["ElementName"]  = switchName + "_External";
                        externalPortToCreate["HostResource"] = new string[] { externalAdapter.Path.Path };

                        //
                        // Clone the externalPort connection and configure it for the internal port.
                        // Use the same switch name, appended with "_Internal", for the port name.
                        // You can use any port name that you like.
                        //
                        using (ManagementObject internalPortToCreate =
                                   (ManagementObject)externalPortToCreate.Clone())
                        {
                            internalPortToCreate["ElementName"]  = switchName + "_Internal";
                            internalPortToCreate["HostResource"] = new string[] { hostComputerSystem.Path.Path };

                            //
                            // Now create the switch with the two ports.
                            //
                            ports = new string[] {
                                externalPortToCreate.GetText(TextFormat.WmiDtd20),
                                internalPortToCreate.GetText(TextFormat.WmiDtd20)
                            };
                        }
                    }

            CreateSwitch(switchName, switchNotes, ports, scope);

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "The external switch '{0}' was created successfully.", switchName));
        }
Exemple #2
0
        public void Get_Win32_LogicalDisk()
        {
            using (ManagementObject obj = new ManagementObject($"Win32_LogicalDisk.DeviceID=\"{WmiTestHelper.SystemDriveId}\""))
            {
                obj.Get();
                Assert.True(obj.Properties.Count > 0);
                Assert.True(ulong.Parse(obj["Size"].ToString()) > 0);
                var classPath = obj.ClassPath.Path;
                Assert.Equal($@"\\{Environment.MachineName}\root\cimv2:Win32_LogicalDisk", classPath);

                var clone = obj.Clone();
                Assert.False(ReferenceEquals(clone, obj));
                ((ManagementObject)clone).Dispose();
            }
        }
Exemple #3
0
 public void EnumerateInstances_For_Win32_LogicalDisk()
 {
     using (var managementClass = new ManagementClass(new ManagementPath("Win32_LogicalDisk")))
         using (ManagementObjectCollection instances = managementClass.GetInstances())
             using (ManagementObjectCollection.ManagementObjectEnumerator instancesEnumerator = instances.GetEnumerator())
             {
                 while (instancesEnumerator.MoveNext())
                 {
                     ManagementObject instance = (ManagementObject)instancesEnumerator.Current;
                     Assert.NotNull(instance);
                     var clone = instance.Clone();
                     Assert.NotNull(clone);
                     Assert.False(ReferenceEquals(instance, clone));
                 }
             }
 }
        public static IResourceAllocationSettingData GetDefaultFromResourcePool(IResourcePool resource)
        {
            ManagementObject managementObject1 = (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)
                        {
                            managementObject1 = relationship;
                            break;
                        }
                        relationship.Dispose();
                    }
                }
            }
            if (managementObject1 == null)
            {
                throw new HyperVException("Unable to find the default settings for the resource");
            }
            string path = (string)managementObject1["PartComponent"];

            managementObject1.Dispose();
            ManagementObject managementObject2 = new ManagementObject(path);

            managementObject2.Scope = ((IWMICommon)resource).Scope;
            managementObject2.Get();
            return((IResourceAllocationSettingData) new ResourceAllocationSettingData((ManagementObject)managementObject2.Clone()));
        }