Home > Virtualization > Removing Virtual Networks : Script

Removing Virtual Networks : Script

 
 

You see this error message because the switch protocol is already enabled on that NIC.  It’s enabled either because there is already a virtual network using that adapter or because something or someone enabled it incorrectly.  If you already have a virtual network using that NIC, you can’t use that NIC on two virtual networks.

 If you ever need to completely remove all virtual networks from your system, you could use one of the options below (I recommend extreme caution ):

Delete all your virtual networks.

I wouldn’t recommend running it remotely because it will tear down your network stack but it should restore it once completed.

/*
Copyright (c) Microsoft Corporation
 
Module Name:
 
    nvspscrub.js
 
*/

//
// VirtualSwitchManagementService object.  Logical wrapper class for Switch Management Service
//
function
VirtualSwitchManagementService(
    Server,
    User,
    Password
    )
{
    //
    // Define instance fields.
    //   
    this.m_VirtualizationNamespace  = null;
   
    this.m_VirtualSwitchManagementService = null;
       

    //
    // Instance methods
    //       
   
    VirtualSwitchManagementService.prototype.DeleteSwitch =
    function(
        VirtualSwitch
        )
       
    /*++

    Description:

        Deletes a virtual switch
       
    Arguments:

        VirtualSwitch – Msvm_VirtualSwitch object to delete

    Return Value:

        SWbemMethod.OutParameters object.

    –*/
   
    {
        var methodName = "DeleteSwitch";

        var inParams = this.m_VirtualSwitchManagementService.Methods_(methodName).inParameters.SpawnInstance_();

        inParams.VirtualSwitch = VirtualSwitch.Path_.Path;
       
        return this.m_VirtualSwitchManagementService.ExecMethod_(methodName, inParams);
    }
   
   
    VirtualSwitchManagementService.prototype.DeleteInternalEthernetPort =
    function(
        InternalEthernetPort
        )

    /*++

    Description:

        Deletes an internal ethernet port
       
    Arguments:

        InternalEthernetPort – Msvm_InternalEthernetPort to delete
       
    Return Value:

        SWbemMethod.OutParameters object.

    –*/

    {
        var methodName = "DeleteInternalEthernetPort";

        var inParams = this.m_VirtualSwitchManagementService.Methods_(methodName).inParameters.SpawnInstance_();
       
        inParams.InternalEthernetPort = InternalEthernetPort.Path_.Path;
       
        return this.m_VirtualSwitchManagementService.ExecMethod_(methodName, inParams);
    }
   
    VirtualSwitchManagementService.prototype.UnbindExternalEthernetPort =
    function(
        ExternalEthernetPort
        )

    /*++

    Description:

        Unbinds an external ethernet port from the virtual network subsystem.  Usually this method
         won’t be called directly
       
    Arguments:

        SwitchPort – Msvm_ExternalEthernetPort to unbind.

    Return Value:

        SWbemMethod.OutParameters object.

    –*/

    {
        var methodName = "UnbindExternalEthernetPort";

        var inParams = this.m_VirtualSwitchManagementService.Methods_(methodName).inParameters.SpawnInstance_();

        inParams.ExternalEthernetPort = ExternalEthernetPort.Path_.Path;
       
        return this.m_VirtualSwitchManagementService.ExecMethod_(methodName, inParams);
    }
   
    //
    // Utility functions
    //
   
    VirtualSwitchManagementService.prototype.WaitForNetworkJob =
    function(
        OutParams
        )

    /*++

    Description:

        WMI calls will exit with some type of return result.  Some will require
        a little more processing before they are complete. This handles those
        states after a wmi call.

    Arguments:

        OutParams – the parameters returned by the wmi call.

    Return Value:

        Status code

    –*/

    {
        if (OutParams.ReturnValue == 4096)
        {
            var jobStateStarting        = 3;
            var jobStateRunning         = 4;
            var jobStateCompleted       = 7;
   
            var networkJob;

            do
            {
                WScript.Sleep(1000);
               
                networkJob = this.m_VirtualizationNamespace.Get(OutParams.Job);

            } while ((networkJob.JobState == jobStateStarting) ||
                     (networkJob.JobState == jobStateRunning));

            if (networkJob.JobState != jobStateCompleted)
            {
                throw(new Error(networkJob.ErrorCode,
                                networkJob.Description + " failed: " + networkJob.ErrorDescription));
            }
           
            return networkJob.ErrorCode;
        }

        return OutParams.ReturnValue;
    }
   
    VirtualSwitchManagementService.prototype.GetSingleObject =
    function(
        SWbemObjectSet
        )

    /*++

    Description:

        Takes a SWbemObjectSet which is expected to have one object and returns the object

    Arguments:

        SWbemObjectSet – The set.

    Return Value:

        The lone member of the set.  Exception thrown if Count does not equal 1.

    –*/

    {
        if (SWbemObjectSet.Count != 1)
        {
            throw(new Error(5, "SWbemObjectSet was expected to have one item but actually had " + SWbemObjectSet.Count));
        }
       
        return SWbemObjectSet.ItemIndex(0);
    }

   
    //
    // Aggregate functions
    //
    VirtualSwitchManagementService.prototype.DeleteSwitchAndWait =
    function(
        VirtualSwitch
        )
       
    /*++

    Description:

        Deletes a switch
       
    Arguments:

        VirtualSwitch – Msvm_VirtualSwitch to delete
       
    Return Value:
   
        None.

    –*/
   
    {
        var outParams = this.DeleteSwitch(VirtualSwitch);

        var wmiRetValue = this.WaitForNetworkJob(outParams);

        if (wmiRetValue != 0)
        {
            throw(new Error(wmiRetValue, "DeleteSwitch failed"));
        }
    }
   
    VirtualSwitchManagementService.prototype.DeleteInternalEthernetPortAndWait =
    function(
        InternalEthernetPort
        )
    /*++

    Description:

        Deletes an internal ethernet port
       
    Arguments:

        InternalEthernetPort – Msvm_InternalEthernetPort to delete
       
    Return Value:

        SWbemMethod.OutParameters object.

    –*/
   
    {
        var outParams = this.DeleteInternalEthernetPort(InternalEthernetPort);

        var wmiRetValue = this.WaitForNetworkJob(outParams);

        if (wmiRetValue != 0)
        {
            throw(new Error(wmiRetValue, "DeleteInternalEthernetPortAndWait failed"));
        }
    }
   
   
    VirtualSwitchManagementService.prototype.UnbindExternalEthernetPortAndWait =
    function(
        ExternalEthernetPort
        )
    /*++

    Description:

        unbinds an internal ethernet port
       
    Arguments:

        ExternalEthernetPort – Msvm_ExternalEthernetPort to unbind
       
    Return Value:

        SWbemMethod.OutParameters object.

    –*/
   
    {
        var outParams = this.UnbindExternalEthernetPort(ExternalEthernetPort);

        var wmiRetValue = this.WaitForNetworkJob(outParams);

        if (wmiRetValue != 0)
        {
            throw(new Error(wmiRetValue, "UnbindExternalEthernetPortAndWait failed"));
        }
    }
   
    //
    // Constructor code
    //
   
    if (Server == null)
    {
        Server = WScript.CreateObject("WScript.Network").ComputerName;
    }
   
    //
    // Set Namespace fields
    //
    try
    {
        var locator = new ActiveXObject("WbemScripting.SWbemLocator");

        this.m_VirtualizationNamespace = locator.ConnectServer(Server, "root\virtualization", User, Password);
    }
    catch (e)
    {
        this.m_VirtualizationNamespace = null;
       
        throw(new Error("Unable to get an instance of Virtualization namespace: " + e.description));
    }
   
    //
    // Set Msvm_VirtualSwitchManagementService field
    //
    try
    {
        var physicalComputerSystem =
                this.m_VirtualizationNamespace.Get(
                        "Msvm_ComputerSystem.CreationClassName=’Msvm_ComputerSystem’,Name=’" + Server + "’");
         
        this.m_VirtualSwitchManagementService = this.GetSingleObject(
                                                        physicalComputerSystem.Associators_(
                                                            "Msvm_HostedService",
                                                            "Msvm_VirtualSwitchManagementService",
                                                            "Dependent"));
    }
    catch (e)
    {
        this.m_VirtualSwitchManagementService = null;
       
        throw(new Error("Unable to get an instance of Msvm_VirtualSwitchManagementService: " + e.description));
    }
}

//
// main
//

var wshShell = WScript.CreateObject("WScript.Shell");

var g_NvspWmi   = null;

Main();

function Main()
{
 WScript.Echo("Looking for nvspwmi…");
 g_NvspWmi   = new VirtualSwitchManagementService();

 WScript.Echo("");
 WScript.Echo("Looking for internal (host) virtual nics…");
 var list = g_NvspWmi.m_VirtualizationNamespace.ExecQuery("SELECT * FROM Msvm_InternalEthernetPort");
 for (i = 0; i < list.Count; i++)
 {
  var next = list.ItemIndex(i);
  WScript.echo(next.DeviceID);
  g_NvspWmi.DeleteInternalEthernetPortAndWait(next);
 }
 
 WScript.Echo("");
 WScript.Echo("Looking for switches…");
 list = g_NvspWmi.m_VirtualizationNamespace.ExecQuery("SELECT * FROM Msvm_VirtualSwitch");
 for (i = 0; i < list.Count; i++)
 {
  var next = list.ItemIndex(i);
  WScript.echo(next.Name);
  g_NvspWmi.DeleteSwitchAndWait(next);
 }
 
 WScript.Echo("");
 WScript.Echo("Looking for external nics…");
 list = g_NvspWmi.m_VirtualizationNamespace.ExecQuery("SELECT * FROM Msvm_ExternalEthernetPort WHERE IsBound=TRUE");
 for (i = 0; i < list.Count; i++)
 {
  var next = list.ItemIndex(i);
  WScript.echo(next.DeviceID);
  g_NvspWmi.UnbindExternalEthernetPortAndWait(next);
 }
 
 WScript.Echo("");
 WScript.Echo("Finished!");
}

 

Categories: Virtualization
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: