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));
        }