ConnectVmToSwitch(
            string virtualMachineName,
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope))

                //
                // Find the Ethernet switch we want to connect to.
                //
                using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                    //
                    // Find the virtual machine we want to connect.
                    //
                    using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(virtualMachineName, scope))

                        //
                        // Get the virtual machine's settings object which is used to make configuration changes.
                        //
                        using (ManagementObject virtualMachineSettings = WmiUtilities.GetVirtualMachineSettings(virtualMachine))

                            //
                            // Add a new synthetic Network Adapter device to the virtual machine.
                            //
                            using (ManagementObject syntheticAdapter = NetworkingUtilities.AddSyntheticAdapter(virtualMachine, scope))

                                //
                                // Now that we have added a network adapter to the virtual machine we can configure its
                                // connection settings.
                                //
                                using (ManagementObject connectionSettingsToAdd =
                                           NetworkingUtilities.GetDefaultEthernetPortAllocationSettingData(scope))
                                {
                                    connectionSettingsToAdd["Parent"]       = syntheticAdapter.Path.Path;
                                    connectionSettingsToAdd["HostResource"] = new string[] { ethernetSwitch.Path.Path };

                                    //
                                    // Now add the connection settings.
                                    //
                                    using (ManagementBaseObject addConnectionInParams =
                                               managementService.GetMethodParameters("AddResourceSettings"))
                                    {
                                        addConnectionInParams["AffectedConfiguration"] = virtualMachineSettings.Path.Path;
                                        addConnectionInParams["ResourceSettings"]      =
                                            new string[] { connectionSettingsToAdd.GetText(TextFormat.WmiDtd20) };

                                        using (ManagementBaseObject addConnectionOutParams =
                                                   managementService.InvokeMethod("AddResourceSettings", addConnectionInParams, null))
                                        {
                                            WmiUtilities.ValidateOutput(addConnectionOutParams, scope);
                                        }
                                    }
                                }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully connected virtual machine '{0}' to switch '{1}'.",
                                            virtualMachineName, switchName));
        }
        DeleteSwitch(
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Find the switch that we want to delete.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                //
                // Now that we have the switch object we can delete it.
                //
                using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                    using (ManagementBaseObject inParams = switchService.GetMethodParameters("DestroySystem"))
                    {
                        inParams["AffectedSystem"] = ethernetSwitch.Path.Path;

                        using (ManagementBaseObject outParams = switchService.InvokeMethod("DestroySystem", inParams, null))
                        {
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "The switch '{0}' was deleted successfully.", switchName));
        }
        ModifySwitchName(
            string existingSwitchName,
            string newSwitchName,
            string newNotes)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Get the switch we want to modify.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(existingSwitchName, scope))

                //
                // To make modifications to the switch, we need to modify its configuration object.
                //
                using (ManagementObject ethernetSwitchSettings = WmiUtilities.GetFirstObjectFromCollection(
                           ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                     "Msvm_SettingsDefineState",
                                                     null, null, null, null, false, null)))
                {
                    //
                    // Modify its properties as desired.
                    //
                    ethernetSwitchSettings["ElementName"] = newSwitchName;
                    ethernetSwitchSettings["Notes"]       = new string[] { newNotes };

                    //
                    // This is how you can modify the IOV Preferred property from its current value.
                    // We leave this commented out because it could potentially fail for a system that doesn't
                    // support IOV.
                    //
                    // ethernetSwitchSettings["IOVPreferred"] = !((bool)ethernetSwitchSettings["IOVPreferred"]);

                    //
                    // Now call the ModifySystemSettings method to apply the changes.
                    //
                    using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                        using (ManagementBaseObject inParams = switchService.GetMethodParameters("ModifySystemSettings"))
                        {
                            inParams["SystemSettings"] = ethernetSwitchSettings.GetText(TextFormat.WmiDtd20);

                            using (ManagementBaseObject outParams =
                                       switchService.InvokeMethod("ModifySystemSettings", inParams, null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "The switch '{0}' was renamed to '{1}' successfully.",
                                            existingSwitchName, newSwitchName));
        }
        ModifyFeatureSettings(
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject managementService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))

                //
                // Find the specified switch.
                //
                using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))
                    using (ManagementObject ethernetSwitchSetting = WmiUtilities.GetFirstObjectFromCollection(
                               ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                         "Msvm_SettingsDefineState",
                                                         null, null, null, null, false, null)))

                        //
                        // Find the existing bandwidth setting and modify it.
                        //
                        using (ManagementObject bandwidthSetting = WmiUtilities.GetFirstObjectFromCollection(
                                   ethernetSwitchSetting.GetRelated("Msvm_VirtualEthernetSwitchBandwidthSettingData",
                                                                    "Msvm_VirtualEthernetSwitchSettingDataComponent",
                                                                    null, null, null, null, false, null)))
                        {
                            //
                            // Increase the current reservation by 1 Mbps.
                            //
                            bandwidthSetting["DefaultFlowReservation"] =
                                (UInt64)bandwidthSetting["DefaultFlowReservation"] + 125000; // in bytes/sec

                            using (ManagementBaseObject inParams =
                                       managementService.GetMethodParameters("ModifyFeatureSettings"))
                            {
                                inParams["FeatureSettings"] = new string[] { bandwidthSetting.GetText(TextFormat.WmiDtd20) };

                                using (ManagementBaseObject outParams =
                                           managementService.InvokeMethod("ModifyFeatureSettings", inParams, null))
                                {
                                    WmiUtilities.ValidateOutput(outParams, scope);
                                }
                            }
                        }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully modified bandwidth feature setting on virtual switch '{0}'.",
                                            switchName));
        }
        AddFeatureSettings(
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            string featureId = NetworkingUtilities.GetSwitchFeatureId(NetworkingUtilities.SwitchFeatureType.Bandwidth);

            using (ManagementObject managementService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))

                //
                // Find the specified switch.
                //
                using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))
                    using (ManagementObject ethernetSwitchSetting = WmiUtilities.GetFirstObjectFromCollection(
                               ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                         "Msvm_SettingsDefineState",
                                                         null, null, null, null, false, null)))

                        //
                        // Add a new bandwidth setting instance.
                        //
                        using (ManagementObject bandwidthSetting =
                                   NetworkingUtilities.GetDefaultFeatureSetting(featureId, scope))
                        {
                            //
                            // Set the default bandwidth reservation to 10 Mbps.
                            //
                            bandwidthSetting["DefaultFlowReservation"] = 12500000; // in bytes/sec

                            using (ManagementBaseObject inParams =
                                       managementService.GetMethodParameters("AddFeatureSettings"))
                            {
                                inParams["AffectedConfiguration"] = ethernetSwitchSetting.Path.Path;
                                inParams["FeatureSettings"]       = new string[] { bandwidthSetting.GetText(TextFormat.WmiDtd20) };

                                using (ManagementBaseObject outParams =
                                           managementService.InvokeMethod("AddFeatureSettings", inParams, null))
                                {
                                    WmiUtilities.ValidateOutput(outParams, scope);
                                }
                            }
                        }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully added bandwidth feature setting to virtual switch '{0}'.",
                                            switchName));
        }
        RemoveFeatureSettings(
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject managementService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))

                //
                // Find the specified switch.
                //
                using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))
                    using (ManagementObject ethernetSwitchSetting = WmiUtilities.GetFirstObjectFromCollection(
                               ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                         "Msvm_SettingsDefineState",
                                                         null, null, null, null, false, null)))

                        //
                        // Find the existing bandwidth setting and remove it.
                        //
                        using (ManagementObject bandwidthSetting = WmiUtilities.GetFirstObjectFromCollection(
                                   ethernetSwitchSetting.GetRelated("Msvm_VirtualEthernetSwitchBandwidthSettingData",
                                                                    "Msvm_VirtualEthernetSwitchSettingDataComponent",
                                                                    null, null, null, null, false, null)))
                        {
                            using (ManagementBaseObject inParams =
                                       managementService.GetMethodParameters("RemoveFeatureSettings"))
                            {
                                inParams["FeatureSettings"] = new string[] { bandwidthSetting.Path.Path };

                                using (ManagementBaseObject outParams =
                                           managementService.InvokeMethod("RemoveFeatureSettings", inParams, null))
                                {
                                    WmiUtilities.ValidateOutput(outParams, scope);
                                }
                            }
                        }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully removed bandwidth feature setting from virtual switch '{0}'.",
                                            switchName));
        }
        AddPorts(
            string switchName,
            string externalAdapterName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            string ethernetSwitchSettingPath;
            string portToCreateText;

            ObjectQuery externalAdapterQuery = new ObjectQuery(string.Format(CultureInfo.InvariantCulture,
                                                                             "select * from Msvm_ExternalEthernetPort where Name=\"{0}\"", externalAdapterName));

            //
            // When modifying the switch, we need to pass in its configuration object.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))
                using (ManagementObject ethernetSwitchSetting = WmiUtilities.GetFirstObjectFromCollection(
                           ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                     "Msvm_SettingsDefineState",
                                                     null, null, null, null, false, null)))
                {
                    ethernetSwitchSettingPath = ethernetSwitchSetting.Path.Path;
                }

            //
            // Get the external adapter that we want to add a connection to.
            //
            using (ManagementObjectSearcher externalAdapterExecute =
                       new ManagementObjectSearcher(scope, externalAdapterQuery))
                using (ManagementObjectCollection externalAdapterCollection = externalAdapterExecute.Get())
                {
                    if (externalAdapterCollection.Count == 0)
                    {
                        throw new ManagementException(string.Format(CultureInfo.InvariantCulture,
                                                                    "There is no external adapter with the name {0}.",
                                                                    externalAdapterName));
                    }

                    using (ManagementObject externalAdapter =
                               WmiUtilities.GetFirstObjectFromCollection(externalAdapterCollection))

                        //
                        // Get the default Msvm_EthernetPortAllocationSettingData instance, which 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 portToCreate =
                                   NetworkingUtilities.GetDefaultEthernetPortAllocationSettingData(scope))
                        {
                            portToCreate["ElementName"]  = switchName + "_External";
                            portToCreate["HostResource"] = new string[] { externalAdapter.Path.Path };

                            portToCreateText = portToCreate.GetText(TextFormat.WmiDtd20);
                        }
                }

            //
            // Now add the port connection to the switch.
            //
            using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                using (ManagementBaseObject inParams =
                           switchService.GetMethodParameters("AddResourceSettings"))
                {
                    inParams["AffectedConfiguration"] = ethernetSwitchSettingPath;
                    inParams["ResourceSettings"]      = new string[] { portToCreateText };

                    using (ManagementBaseObject outParams =
                               switchService.InvokeMethod("AddResourceSettings", inParams, null))
                    {
                        WmiUtilities.ValidateOutput(outParams, scope);
                    }
                }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "We added an external port connection to '{0}' on the switch '{1}'.",
                                            externalAdapterName, switchName));
        }
        RemovePorts(
            string switchName)
        {
            ManagementScope scope         = new ManagementScope(@"root\virtualization\v2");
            List <string>   portsToDelete = new List <string>();

            //
            // Find the switch that we want to modify.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                //
                // Find its internal and/or external ports to delete, but don't delete any ports that
                // are connected to virtual machines.
                // Internal ports can be recognized because their HostResource property is set to the
                // host computer.
                // Likewise, external ports can be recognized through their HostResource property,
                // which is always set to the Msvm_ExternalEthernetPort it is connected to.
                //
                using (ManagementObjectCollection ports = ethernetSwitch.GetRelated("Msvm_EthernetSwitchPort",
                                                                                    "Msvm_SystemDevice",
                                                                                    null, null, null, null, false, null))
                {
                    foreach (ManagementObject port in ports)
                    {
                        using (port)
                        {
                            //
                            // The port's connection settings are stored on its related
                            // Msvm_EthernetPortAllocationSettingData object.
                            //
                            using (ManagementObject portSettings = WmiUtilities.GetFirstObjectFromCollection(
                                       port.GetRelated("Msvm_EthernetPortAllocationSettingData",
                                                       "Msvm_ElementSettingData",
                                                       null, null, null, null, false, null)))
                            {
                                string[] hostResource = (string[])portSettings["HostResource"];

                                if (hostResource != null && hostResource.Length > 0)
                                {
                                    ManagementPath hostResourcePath = new ManagementPath(hostResource[0]);

                                    if (string.Equals(hostResourcePath.ClassName, "Msvm_ComputerSystem",
                                                      StringComparison.OrdinalIgnoreCase))
                                    {
                                        // This is the internal port. Add it to the list to delete.
                                        portsToDelete.Add(portSettings.Path.Path);
                                    }
                                    else if (string.Equals(hostResourcePath.ClassName, "Msvm_ExternalEthernetPort",
                                                           StringComparison.OrdinalIgnoreCase))
                                    {
                                        // This is the external port. Add it to the list to delete.
                                        portsToDelete.Add(portSettings.Path.Path);
                                    }
                                }
                            }
                        }
                    }
                }

            if (portsToDelete.Count == 0)
            {
                throw new ManagementException(string.Format(CultureInfo.InvariantCulture,
                                                            "The switch '{0}' does not have any internal or external ports to remove.",
                                                            switchName));
            }

            //
            // Now that we have the ports we want to delete we can call the RemoveResourceSettings
            // method.
            //
            using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                using (ManagementBaseObject inParams = switchService.GetMethodParameters("RemoveResourceSettings"))
                {
                    inParams["ResourceSettings"] = portsToDelete.ToArray();

                    using (ManagementBaseObject outParams = switchService.InvokeMethod("RemoveResourceSettings", inParams, null))
                    {
                        WmiUtilities.ValidateOutput(outParams, scope);
                    }
                }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "We removed the internal and external ports from switch '{0}' successfully.", switchName));
        }
        ModifyPorts(
            string switchName,
            string newExternalAdapterName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            ObjectQuery externalAdapterQuery = new ObjectQuery(string.Format(CultureInfo.InvariantCulture,
                                                                             "select * from Msvm_ExternalEthernetPort where Name=\"{0}\"", newExternalAdapterName));

            //
            // Find the switch that we want to modify.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                //
                // Get the external adapter we are connecting to.
                //
                using (ManagementObjectSearcher externalAdapterExecute = new ManagementObjectSearcher(scope, externalAdapterQuery))
                    using (ManagementObjectCollection externalAdapterCollection = externalAdapterExecute.Get())
                    {
                        if (externalAdapterCollection.Count == 0)
                        {
                            throw new ManagementException("There is no external adapter with the name: " + newExternalAdapterName);
                        }

                        string externalAdapterPath;
                        string externalPortSettingsText = null;

                        using (ManagementObject externalAdapter = WmiUtilities.GetFirstObjectFromCollection(externalAdapterCollection))
                        {
                            externalAdapterPath = externalAdapter.Path.Path;
                        }

                        //
                        // Find the switch's current external port.
                        //

                        using (ManagementObjectCollection ports = ethernetSwitch.GetRelated("Msvm_EthernetSwitchPort",
                                                                                            "Msvm_SystemDevice",
                                                                                            null, null, null, null, false, null))
                            foreach (ManagementObject port in ports)
                            {
                                using (port)
                                {
                                    //
                                    // The port's connection settings are stored on its related
                                    // Msvm_EthernetPortAllocationSettingData object.
                                    // The external port is the one connected to a Msvm_ExternalEthernetPort
                                    // through its HostResource property.
                                    //
                                    using (ManagementObject portSettings = WmiUtilities.GetFirstObjectFromCollection(
                                               port.GetRelated("Msvm_EthernetPortAllocationSettingData",
                                                               "Msvm_ElementSettingData",
                                                               null, null, null, null, false, null)))
                                    {
                                        string[] hostResource = (string[])portSettings["HostResource"];

                                        if (hostResource != null && hostResource.Length > 0)
                                        {
                                            ManagementPath hostResourcePath = new ManagementPath(hostResource[0]);

                                            if (string.Equals(hostResourcePath.ClassName, "Msvm_ExternalEthernetPort",
                                                              StringComparison.OrdinalIgnoreCase))
                                            {
                                                //
                                                // Validate that it isn't already connected to the external adapter we
                                                // are trying to change it to.
                                                //
                                                if (string.Equals(hostResourcePath.Path, externalAdapterPath,
                                                                  StringComparison.OrdinalIgnoreCase))
                                                {
                                                    throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
                                                                                                "The switch '{0}' is already connected to '{1}'.",
                                                                                                switchName, newExternalAdapterName));
                                                }

                                                //
                                                // Now that we have the switch's external port, we can modify it so that it
                                                // is connected to newExternalAdapterName.
                                                //

                                                portSettings["HostResource"] = new string[] { externalAdapterPath };
                                                externalPortSettingsText     = portSettings.GetText(TextFormat.WmiDtd20);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                        if (externalPortSettingsText == null)
                        {
                            throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
                                                                        "The switch '{0}' is not connected to an external network.", switchName));
                        }

                        using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                            using (ManagementBaseObject inParams = switchService.GetMethodParameters("ModifyResourceSettings"))
                            {
                                inParams["ResourceSettings"] = new string[] { externalPortSettingsText };

                                using (ManagementBaseObject outParams =
                                           switchService.InvokeMethod("ModifyResourceSettings", inParams, null))
                                {
                                    WmiUtilities.ValidateOutput(outParams, scope);
                                }
                            }
                    }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully bound the switch '{0}' to the external network '{1}'.",
                                            switchName, newExternalAdapterName));
        }
        EnumerateSwitch(
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Initialize our SwitchInfo structure to hold information about the switch.
            //
            SwitchInfo ethernetSwitchInfo = new SwitchInfo();

            ethernetSwitchInfo.Name              = switchName;
            ethernetSwitchInfo.Type              = SwitchConnectionType.Private;
            ethernetSwitchInfo.PortList          = new List <PortInfo>();
            ethernetSwitchInfo.SwitchFeatureList = new List <NetworkingUtilities.SwitchFeatureType>();

            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))
            {
                //
                // Enumerate the switch's ports.
                //
                using (ManagementObjectCollection portCollection = ethernetSwitch.GetRelated("Msvm_EthernetSwitchPort",
                                                                                             "Msvm_SystemDevice",
                                                                                             null, null, null, null, false, null))
                {
                    foreach (ManagementObject port in portCollection)
                    {
                        using (port)
                        {
                            //
                            // Initialize a PortInfo structure to hold information about this port.
                            //
                            PortInfo portInfo = new PortInfo();
                            portInfo.Type        = PortConnectionType.Nothing;
                            portInfo.FeatureList = new List <NetworkingUtilities.PortFeatureType>();

                            //
                            // The port's connection settings are stored on its related
                            // Msvm_EthernetPortAllocationSettingData object.
                            //
                            using (ManagementObject portSettings = WmiUtilities.GetFirstObjectFromCollection(
                                       port.GetRelated("Msvm_EthernetPortAllocationSettingData",
                                                       "Msvm_ElementSettingData",
                                                       null, null, null, null, false, null)))
                            {
                                //
                                // Determine the port's connection type.
                                //
                                portInfo.Type = DeterminePortType(portSettings);

                                if (portInfo.Type == PortConnectionType.VirtualMachine)
                                {
                                    // Get the name of the connected virtual machine.
                                    using (ManagementObject virtualMachineSettings = WmiUtilities.GetFirstObjectFromCollection(
                                               portSettings.GetRelated("Msvm_VirtualSystemSettingData",
                                                                       "Msvm_VirtualSystemSettingDataComponent",
                                                                       null, null, null, null, false, null)))
                                    {
                                        portInfo.ConnectedName = (string)virtualMachineSettings["ElementName"];
                                    }
                                }
                                else if (portInfo.Type == PortConnectionType.External)
                                {
                                    // Get the name of the external connection.
                                    using (ManagementObject externalAdapter = new ManagementObject(
                                               ((string[])portSettings["HostResource"])[0]))
                                    {
                                        portInfo.ConnectedName = (string)externalAdapter["ElementName"];
                                    }
                                }

                                //
                                // Now determine which advanced properties are configured for this port.
                                // Each Feature has its own class definition and is related to the portSettings
                                // through the Msvm_EthernetPortSettingDataComponent association.
                                //
                                using (ManagementObjectCollection portFeatureCollection =
                                           portSettings.GetRelated("Msvm_EthernetSwitchPortFeatureSettingData",
                                                                   "Msvm_EthernetPortSettingDataComponent",
                                                                   null, null, null, null, false, null))
                                {
                                    foreach (ManagementObject portFeature in portFeatureCollection)
                                    {
                                        using (portFeature)
                                        {
                                            portInfo.FeatureList.Add(DeterminePortFeatureType(portFeature));
                                        }
                                    }
                                }
                            }

                            ethernetSwitchInfo.PortList.Add(portInfo);
                        }
                    }
                }

                //
                // Then enumerate the switch's features.
                //
                using (ManagementObject ethernetSwitchSetting = WmiUtilities.GetFirstObjectFromCollection(
                           ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                     "Msvm_SettingsDefineState",
                                                     null, null, null, null, false, null)))
                    using (ManagementObjectCollection switchFeatures = ethernetSwitchSetting.GetRelated(
                               "Msvm_EthernetSwitchFeatureSettingData",
                               "Msvm_VirtualEthernetSwitchSettingDataComponent",
                               null, null, null, null, false, null))
                        foreach (ManagementObject switchFeature in switchFeatures)
                        {
                            using (switchFeature)
                            {
                                ethernetSwitchInfo.SwitchFeatureList.Add(DetermineSwitchFeatureType(switchFeature));
                            }
                        }
            }

            //
            // Now that we have enumerated all of the switch's ports, we can determine the
            // switch's connection type.
            //
            ethernetSwitchInfo.Type = DetermineSwitchConnectionType(ethernetSwitchInfo.PortList);

            //
            // We now have all of the information we need - output it to the console.
            //
            OutputSwitchInfo(ethernetSwitchInfo);
        }
        DisconnectVmFromSwitch(
            string virtualMachineName,
            string switchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope))

                //
                // Find the Ethernet switch we want to disconnect from.
                //
                using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                    //
                    // Find the virtual machine we want to disconnect.
                    //
                    using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(virtualMachineName, scope))
                    {
                        //
                        // Find all of the connections to this switch.
                        //
                        IList <ManagementObject> connectionsToSwitch =
                            NetworkingUtilities.FindConnectionsToSwitch(virtualMachine, ethernetSwitch, scope);

                        //
                        // Now that we have found all of the connections to the switch we can go through and
                        // disable each connection so that it is not connected to anything. Note that you can also
                        // delete the connection object, but disabling the connection is sometimes preferable because
                        // it preserves all of the connection's configuration options along with any metrics
                        // associated with the connection.
                        //
                        try
                        {
                            foreach (ManagementObject connection in connectionsToSwitch)
                            {
                                connection["EnabledState"] = 3; // 3 means "Disabled"

                                using (ManagementBaseObject inParams =
                                           managementService.GetMethodParameters("ModifyResourceSettings"))
                                {
                                    inParams["ResourceSettings"] = new string[] { connection.GetText(TextFormat.WmiDtd20) };

                                    using (ManagementBaseObject outParams =
                                               managementService.InvokeMethod("ModifyResourceSettings", inParams, null))
                                    {
                                        WmiUtilities.ValidateOutput(outParams, scope);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            // Dispose of the connectionsToSwitch.
                            foreach (ManagementObject connection in connectionsToSwitch)
                            {
                                connection.Dispose();
                            }
                        }
                    }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully disconnected virtual machine '{0}' from switch '{1}'.",
                                            virtualMachineName, switchName));
        }
        ModifyConnection(
            string virtualMachineName,
            string currentSwitchName,
            string newSwitchName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope))

                //
                // Find the Ethernet switch we want to disconnect from and the one we want to connect to.
                //
                using (ManagementObject currentEthernetSwitch = NetworkingUtilities.FindEthernetSwitch(currentSwitchName, scope))
                    using (ManagementObject newEthernetSwitch = NetworkingUtilities.FindEthernetSwitch(newSwitchName, scope))

                        //
                        // Find the virtual machine we want to modify.
                        //
                        using (ManagementObject virtualMachine = WmiUtilities.GetVirtualMachine(virtualMachineName, scope))
                        {
                            //
                            // Find the connections to the current switch.
                            // Note that this method finds all connections to the switch, including those connections
                            // that are configured to dynamically select a switch from a switch resource pool and
                            // reconfigures them to always connect to the new switch. If you only want to modify
                            // connections that are configured with a hard-affinity to the original switch, modify the
                            // NetworkingUtilities.FindConnectionsToSwitch method.
                            //
                            IList <ManagementObject> currentConnections =
                                NetworkingUtilities.FindConnectionsToSwitch(virtualMachine, currentEthernetSwitch, scope);

                            //
                            // Set each connection to connect to the new switch. If the virtual machine is currently
                            // running, then it will immediately connect to the new switch. If it is not running, then
                            // it will connect to the switch when it is turned on.
                            //
                            try
                            {
                                foreach (ManagementObject connection in currentConnections)
                                {
                                    connection["HostResource"] = new string[] { newEthernetSwitch.Path.Path };

                                    using (ManagementBaseObject inParams =
                                               managementService.GetMethodParameters("ModifyResourceSettings"))
                                    {
                                        inParams["ResourceSettings"] = new string[] { connection.GetText(TextFormat.WmiDtd20) };

                                        using (ManagementBaseObject outParams =
                                                   managementService.InvokeMethod("ModifyResourceSettings", inParams, null))
                                        {
                                            WmiUtilities.ValidateOutput(outParams, scope);
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                // Dispose of the connections.
                                foreach (ManagementObject connection in currentConnections)
                                {
                                    connection.Dispose();
                                }
                            }
                        }

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "Successfully modified virtual machine '{0}' so that every connection to '{1}' is now " +
                                            "connected to '{2}'.", virtualMachineName, currentSwitchName, newSwitchName));
        }
        SetExtensionEnabledState(
            string switchName,
            string extensionName,
            bool enabled)
        {
            bool found = false;

            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Get the switch we want to modify.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                //
                // To make modifications to the switch, we need to modify its configuration object.
                //
                using (ManagementObjectCollection extensions = ethernetSwitch.GetRelated(
                           "Msvm_EthernetSwitchExtension",
                           "Msvm_HostedEthernetSwitchExtension",
                           null,
                           null,
                           null,
                           null,
                           false,
                           null))
                {
                    foreach (ManagementObject extension in extensions)
                    {
                        using (extension)
                        {
                            if (!String.Equals(
                                    (string)extension["ElementName"],
                                    extensionName,
                                    StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            found = true;

                            if ((UInt16)extension["EnabledState"] == CimEnabledStateNotApplicable)
                            {
                                Console.WriteLine("The enabled state of the specified extension cannot be changed.");
                                continue;
                            }

                            if (((UInt16)extension["EnabledState"] == CimEnabledStateEnabled) && enabled)
                            {
                                Console.WriteLine("The specified extension is already enabled.");
                                continue;
                            }

                            if (((UInt16)extension["EnabledState"] == CimEnabledStateDisabled) && !enabled)
                            {
                                Console.WriteLine("The specified extension is already disabled.");
                                continue;
                            }

                            using (ManagementBaseObject inParams = extension.GetMethodParameters("RequestStateChange"))
                            {
                                inParams["RequestedState"] = enabled ? CimEnabledStateEnabled : CimEnabledStateDisabled;

                                using (ManagementBaseObject outParams =
                                           extension.InvokeMethod("RequestStateChange", inParams, null))
                                {
                                    WmiUtilities.ValidateOutput(outParams, scope);

                                    Console.WriteLine(string.Format(
                                                          CultureInfo.CurrentCulture,
                                                          "Extension '{0}' was successfully {1} on switch '{2}'.",
                                                          extensionName,
                                                          enabled ? "enabled" : "disabled",
                                                          switchName));
                                }
                            }
                        }
                    }
                }

            if (!found)
            {
                throw new ApplicationException(String.Format(
                                                   CultureInfo.CurrentCulture,
                                                   "Could not find extension '{0}' on switch '{1}'.",
                                                   extensionName,
                                                   switchName));
            }
        }
        MoveExtension(
            string switchName,
            string extensionName,
            int offset)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Get the switch we want to modify.
            //
            using (ManagementObject ethernetSwitch = NetworkingUtilities.FindEthernetSwitch(switchName, scope))

                //
                // To make modifications to the switch, we need to modify its configuration object.
                //
                using (ManagementObject ethernetSwitchSettings = WmiUtilities.GetFirstObjectFromCollection(
                           ethernetSwitch.GetRelated("Msvm_VirtualEthernetSwitchSettingData",
                                                     "Msvm_SettingsDefineState",
                                                     null, null, null, null, false, null)))
                {
                    string[] extensionOrder = (string[])ethernetSwitchSettings["ExtensionOrder"];
                    byte[]   extensionTypes = new byte[extensionOrder.Length];
                    int      extensionIndex = -1;

                    for (int idx = 0; idx < extensionOrder.Length; ++idx)
                    {
                        using (ManagementObject extension = new ManagementObject(extensionOrder[idx]))
                        {
                            extension.Get();

                            extensionTypes[idx] = (byte)extension["ExtensionType"];

                            if (String.Equals(
                                    (string)extension["ElementName"],
                                    extensionName,
                                    StringComparison.OrdinalIgnoreCase))
                            {
                                extensionIndex = idx;
                            }
                        }
                    }

                    if (extensionIndex == -1)
                    {
                        throw new ApplicationException(String.Format(
                                                           CultureInfo.CurrentCulture,
                                                           "Could not find extension '{0}' on switch '{1}'.",
                                                           extensionName,
                                                           switchName));
                    }

                    //
                    // Ensure that this is a valid move operation. The new extension index must
                    // be in range, and the extension cannot be interleaved with other extensions of
                    // a different type.
                    //
                    int newExtensionIndex = extensionIndex + offset;

                    if ((newExtensionIndex < 0) || (newExtensionIndex >= extensionOrder.Length))
                    {
                        throw new ApplicationException("Invalid move operation.");
                    }

                    if (extensionTypes[newExtensionIndex] != extensionTypes[extensionIndex])
                    {
                        throw new ApplicationException("Invalid move operation.");
                    }

                    //
                    // Reorder the extensions and apply the switch settings changes.
                    //
                    string temp = extensionOrder[newExtensionIndex];
                    extensionOrder[newExtensionIndex] = extensionOrder[extensionIndex];
                    extensionOrder[extensionIndex]    = temp;

                    ethernetSwitchSettings["ExtensionOrder"] = extensionOrder;

                    using (ManagementObject switchService = NetworkingUtilities.GetEthernetSwitchManagementService(scope))
                        using (ManagementBaseObject inParams = switchService.GetMethodParameters("ModifySystemSettings"))
                        {
                            inParams["SystemSettings"] = ethernetSwitchSettings.GetText(TextFormat.WmiDtd20);

                            using (ManagementBaseObject outParams =
                                       switchService.InvokeMethod("ModifySystemSettings", inParams, null))
                            {
                                WmiUtilities.ValidateOutput(outParams, scope);
                            }
                        }
                }

            Console.WriteLine(string.Format(
                                  CultureInfo.CurrentCulture,
                                  "Extension '{0}' was successfully reordered on switch '{1}'.",
                                  extensionName,
                                  switchName));
        }