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));
        }
        CreateExternalOnlySwitch(
            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 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 portToCreate =
                           NetworkingUtilities.GetDefaultEthernetPortAllocationSettingData(scope))
                {
                    portToCreate["ElementName"]  = switchName + "_External";
                    portToCreate["HostResource"] = new string[] { externalAdapter.Path.Path };

                    //
                    // Now create the switch with the external port.
                    //
                    ports = new string[] { portToCreate.GetText(TextFormat.WmiDtd20) };
                }

            CreateSwitch(switchName, switchNotes, ports, scope);

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "The external-only switch '{0}' was created successfully.", switchName));
        }
        SupportsTrunkMode(
            string externalAdapterName)
        {
            ManagementScope scope             = new ManagementScope(@"root\virtualization\v2");
            bool            supportsTrunkMode = false;

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

                //
                // From the external adapter we need to follow the associations to get to the
                // Msvm_VLANEndpoint object that has the property we can query to see if the
                // adapter supports trunk mode. Note however, that these associated objects only
                // exist if the adapter is connected to a switch. Until the adapter is connected to
                // a switch there is not a way through the Hyper-V WMI API to determine whether it
                // supports trunk mode.
                //
                using (ManagementObjectCollection lanEndpointCollection = externalAdapter.GetRelated("Msvm_LanEndpoint"))
                {
                    if (lanEndpointCollection.Count == 0)
                    {
                        throw new ManagementException("This external adapter is not connected to any switch. " +
                                                      "Cannot determine trunk mode support.");
                    }

                    using (ManagementObject lanEndpoint = WmiUtilities.GetFirstObjectFromCollection(
                               lanEndpointCollection))

                        using (ManagementObject otherLanEndpoint = WmiUtilities.GetFirstObjectFromCollection(
                                   lanEndpoint.GetRelated("Msvm_LanEndpoint")))

                            using (ManagementObject vlanEndpoint = WmiUtilities.GetFirstObjectFromCollection(
                                       otherLanEndpoint.GetRelated("Msvm_VLANEndpoint")))
                            {
                                //
                                // Now that we have the VLAN Endpoint, we can check its SupportedEndpointModes
                                // property.
                                //
                                ushort[] supportedEndpointModes = (ushort[])vlanEndpoint["SupportedEndpointModes"];
                                foreach (ushort supportedMode in supportedEndpointModes)
                                {
                                    if (supportedMode == 5) // 5 means "TrunkMode"
                                    {
                                        supportsTrunkMode = true;
                                        break;
                                    }
                                }
                            }
                }

            if (supportsTrunkMode)
            {
                Console.WriteLine("Successfully determined that the external adapter '{0}' does support trunk mode.",
                                  externalAdapterName);
            }
            else
            {
                Console.WriteLine("Successfully determined that the external adapter '{0}' does NOT support trunk mode.",
                                  externalAdapterName);
            }
        }